第一个脚本附加到一个空的GameObject。
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;
}
}第二个脚本被附加到GameObject/s上,我想提供信息。因此,这个脚本被单独地附加到每个GameObject上。
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一直为null:
我在做:
_rotate = new Rotate>();但这里仍然是空的:
_rotate.rotationSpeed = _objecttorotate.rotationSpeed;发布于 2017-07-23 15:18:20
我想你不明白团结是怎么运作的。
首先,_rotate = new Rotate>();不是有效的C#,将引发错误。
其次,在您的示例中,Rotate是一个没有附加到GameObject的MonoBehaviour。我认为无论你想要实现什么,也许都是一步之遥。您可以将一个独立的Update-call (我甚至不知道它是否得到了它的Update-method调用)与另一个对象同步。简而言之:在我看来,你的代码似乎是胡说八道。
我建议您将RotateObject方法移动到SpinableObject中,并从SpinObject调用它,而不是将内容推入_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();
}
}https://stackoverflow.com/questions/45266532
复制相似问题