C# 如何使用脚本为其他脚本设置提供信息?
第一个脚本附加到一个空的游戏对象C# 如何使用脚本为其他脚本设置提供信息?,c#,unity3d,unity5,C#,Unity3d,Unity5,第一个脚本附加到一个空的游戏对象 using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializable] public class SpinableObject { public Transform t; public float rotationSpeed; public float minSpeed; public float m
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class SpinableObject
{
public Transform t;
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
}
public class SpinObject : MonoBehaviour
{
public SpinableObject[] objectsToRotate;
private Rotate _rotate;
private int index = 0;
// Use this for initialization
void Start()
{
_rotate = new Rotate>();
}
// Update is called once per frame
void Update()
{
var _objecttorotate = objectsToRotate[index];
_rotate.rotationSpeed = _objecttorotate.rotationSpeed;
_rotate.minSpeed = _objecttorotate.minSpeed;
_rotate.maxSpeed = _objecttorotate.maxSpeed;
_rotate.speedRate = _objecttorotate.speedRate;
_rotate.slowDown = _objecttorotate.slowDown;
}
}
第二个脚本附加到我想要提供信息的游戏对象。因此,该脚本分别附加到每个游戏对象
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour
{
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
RotateObject();
}
public void RotateObject()
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;
rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
transform.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
但这里仍然是空的:
_rotate.rotationSpeed = _objecttorotate.rotationSpeed;
我想你不明白团结是如何运作的 首先,
\u rotate=new rotate>()代码>无效,将抛出错误
其次,在你的例子中,Rotate
是一个monobhavior
,它没有附加到GameObject
。我认为,无论你试图实现什么,都可能是一步之遥。您可以将已解除连接的组件的Update
-调用(我甚至不知道它是否调用了Update
-方法)与另一个对象同步。简而言之:我觉得你的代码毫无意义
我建议您将RotateObject
方法移动到SpinableObject
中,并从SpinableObject
调用它,而不是将内容推入\u rotate
。这应该行得通
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class SpinableObject
{
public Transform t;
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
public void RotateObject()
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;
rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
public class SpinObject : MonoBehaviour
{
[SerializeField]
private SpinableObject[] objectsToRotate;
// Update is called once per frame
void Update()
{
foreach(var spinner in objectsToRotate)
spinner.RotateObject();
}
}
使用System.Collections.Generic;
使用UnityEngine;
[系统可序列化]
公共类SpinableObject
{
公共交通;
公众浮标轮换速度;
公共浮动速度;
公共浮动最大速度;
公共浮动汇率;
公共利益减速;
public void RotateObject()
{
如果(旋转速度>最大速度)
减速=真;
否则如果(旋转速度<分钟速度)
减速=错误;
旋转速度=(减速)?旋转速度-0.1f:旋转速度+0.1f;
t、 旋转(Vector3.forward、Time.deltaTime*旋转速度);
}
}
公共类SpinObject:单行为
{
[序列化字段]
私有SpinableObject[]objectsToRotate;
//每帧调用一次更新
无效更新()
{
foreach(objectsToRotate中的变量微调器)
旋转对象();
}
}
这正在工作。如果我想添加一些全局变量?我的意思是,变量现在将单独影响每个变换,但全局变量将影响所有变换。例如,如果在数组中有5个对象,那么如果我使用全局变量,它将同时影响所有5个游戏对象,如果不使用全局变量,数组中的每个游戏对象将使用自己的变量。也许可以使用一些bool,比如:globalEffect?将“全局”变量添加到SpinObject
,然后将值传递到RotateObject()
@daniel lee:您已经在StackOverflow上呆了10天,并且发布了11个问题。你似乎要求社区为你做所有的工作。
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class SpinableObject
{
public Transform t;
public float rotationSpeed;
public float minSpeed;
public float maxSpeed;
public float speedRate;
public bool slowDown;
public void RotateObject()
{
if (rotationSpeed > maxSpeed)
slowDown = true;
else if (rotationSpeed < minSpeed)
slowDown = false;
rotationSpeed = (slowDown) ? rotationSpeed - 0.1f : rotationSpeed + 0.1f;
t.Rotate(Vector3.forward, Time.deltaTime * rotationSpeed);
}
}
public class SpinObject : MonoBehaviour
{
[SerializeField]
private SpinableObject[] objectsToRotate;
// Update is called once per frame
void Update()
{
foreach(var spinner in objectsToRotate)
spinner.RotateObject();
}
}