首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用脚本来提供另一个脚本设置?

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

Stack Overflow用户
提问于 2017-07-23 15:00:38
回答 1查看 43关注 0票数 0

第一个脚本附加到一个空的GameObject。

代码语言:javascript
复制
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上。

代码语言:javascript
复制
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:

我在做:

代码语言:javascript
复制
_rotate = new Rotate>();

但这里仍然是空的:

代码语言:javascript
复制
_rotate.rotationSpeed = _objecttorotate.rotationSpeed;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-23 15:18:20

我想你不明白团结是怎么运作的。

首先,_rotate = new Rotate>();不是有效的C#,将引发错误。

其次,在您的示例中,Rotate是一个没有附加到GameObjectMonoBehaviour。我认为无论你想要实现什么,也许都是一步之遥。您可以将一个独立的Update-call (我甚至不知道它是否得到了它的Update-method调用)与另一个对象同步。简而言之:在我看来,你的代码似乎是胡说八道。

我建议您将RotateObject方法移动到SpinableObject中,并从SpinObject调用它,而不是将内容推入_rotate。这应该能行。

代码语言:javascript
复制
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();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45266532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档