如何使用脚本提供其他脚本设置?

时间:2017-07-23 15:00:38

标签: c# unity3d unity5

第一个脚本附加到空的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;

1 个答案:

答案 0 :(得分:1)

我不认为你理解Unity的运作方式。

首先,_rotate = new Rotate>();无效C#并会抛出错误。

其次,在你的情况下,Rotate是一个MonoBehaviour,它没有附加到GameObject。我认为,无论你想做什么,都可能是迈向远的一步。你可以&#39;同步Update - 一个脱离Component的调用(我甚至不知道它是否得到它的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();
    }
}