C# 实例化的预置应该从主脚本调用函数吗?
我想要这样的东西 ComponentTest.cs-此脚本附加到场景中的空游戏对象C# 实例化的预置应该从主脚本调用函数吗?,c#,unity3d,gameobject,C#,Unity3d,Gameobject,我想要这样的东西 ComponentTest.cs-此脚本附加到场景中的空游戏对象 public class ComponentTest : MonoBehaviour { public GameObject boxPrefab; void Start () { GameObject temp = Instantiate(boxPrefab, new Vector3(0,0,0), Quaternion.identity) as GameObject;
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");
}
}
我认为这是一个错误:
成员'prefableBox.go'不能用作方法或委托
首先,
go
对象被声明为一个游戏对象,但是你给它分配了一个函数,然后像函数一样调用它
看起来您需要闭包,在闭包中可以将函数存储到变量,并以您的方式调用它们
这篇文章包括:
似乎数据类型需要是Action
,但最好仔细阅读以正确掌握它
祝你好运
public class PrefabBox : MonoBehaviour {
public GameObject go;
void Start () {
go ();
}
}