[英]Passing values from one script to another without a game object in the scene
当层次结构中存在游戏对象时,我发现了很多有关将参数从一个脚本传递到另一个脚本的信息。 我的问题是我的Transform对象是使用Instantiate(myPrefab)动态创建的。 有什么方法可以从另一个脚本访问myPrefab游戏对象的位置?
您可以在实例化GameObject之后对其进行存储(请参见下面的示例)。 如果还有更多GameObject,请使用列表存储它们。
在您GetGOPosition()
地方调用InstantiateGO()
和GetGOPosition()
。
public class YourClass: MonoBehaviour
{
public GameObject yourPrefab;
public GameObject yourGameObject;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void InstantiateGO()
{
yourGameObject = Instantiate(yourPrefab); // assign the newly instantiated GameObject to yourGameObject
}
void GetGOPosition()
{
var x = yourGameObject.position;
//Do something here
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.