【问题标题】:Instantiated prefab should call a function from the main script?实例化的预制件应该从主脚本中调用一个函数吗?
【发布时间】:2015-10-26 15:44:11
【问题描述】:

我想要这样的东西。

ComponentTest.cs - 此脚本附加到场景中的空游戏对象。

public class ComponentTest : MonoBehaviour {

    public GameObject boxPrefab;

    void Start () {

        GameObject temp = Instantiate(boxPrefab, new Vector3(0,0,0), Quaternion.identity) as GameObject;

        temp.GetComponent<PrefabBox> ().go = gameObject.GetComponent<ComponentTest>().Yes();

    }

    public void Yes(){
        print("yes");
    }

}

和 PrefabBox.cs - 此脚本附加到预制件。

public class PrefabBox : MonoBehaviour {

    public GameObject go;

    void Start () {
        go ();
    }

}

我认为这是错误:

成员 `PrefabBox.go' 不能用作方法或委托

【问题讨论】:

    标签: c# unity3d gameobject


    【解决方案1】:

    首先,go 对象被声明为 GameObject,然后你为其分配一个函数,然后像函数一样调用它。

    看起来您需要闭包,您可以在其中将函数存储到变量并以您的方式调用它们。

    这篇文章涵盖了它:http://answers.unity3d.com/questions/494640/capturing-a-variable-in-a-closure-behaves-differen.html

    似乎数据类型需要是Action,但最好阅读它以正确掌握它。

    祝你好运。

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      • 2012-06-05
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多