我如何使用脚本来提供另一个脚本设置?

How can i use a script to feed another script settings?

第一个脚本附加到一个空的游戏对象。

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 我想提供信息。所以这个脚本是单独附加到每个游戏对象上的。

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;

我认为您不了解 Unity 的工作原理。

首先,_rotate = new Rotate>(); 不是有效的 C#,会抛出错误。

其次,在你的例子中 Rotate 是一个 MonoBehaviour,它没有附加到 GameObject。我认为无论你试图完成什么,都可能是一个很远的步骤。您根本无法将分离的 ComponentUpdate 调用(我什至不知道它是否调用了 Update 方法)与另一个对象同步。简而言之:您的代码对我来说似乎是胡说八道。

我建议您将 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();
    }
}