[英]unity/c#: data from another gameobject
I want in my Unity game, to get a float of another GameObject.我想在我的 Unity 游戏中获得另一个 GameObject 的浮动。 The Gameonject is called "Spawner", the script on the GameObject "CarZSpawner" and the float in the script "mult".
Gameonject 称为“Spawner”,GameObject 上的脚本为“CarZSpawner”,脚本中的浮点数为“mult”。 The float "mult ist changing every few seconds."
浮点数“每隔几秒就会改变一次”。
i tried many things but nothing works, and i can't find anything on the internet:(我尝试了很多东西,但没有任何效果,我在互联网上找不到任何东西:(
public class TrainScript : MonoBehaviour
{
void Update()
{
float test = GameObject.Find("Spawner").GetComponent<CarScriptZ.mult>()
}
}
Thanks for help:)感谢帮助:)
I'm pretty that your script is called CarScriptZ
and mult
is rather the field you are trying to access so it should be我很高兴您的脚本被称为
CarScriptZ
并且mult
是您尝试访问的字段,所以它应该是
float test = GameObject.Find("Spawner").GetComponent<CarScriptZ>().mult;
Besides that you should store the references once eg in Start
and then re-use it later like除此之外,您应该将引用存储一次,例如在
Start
中,然后稍后重新使用它
// If possible already reference this via the Inspector by drag & drop
[SerializeField] private CarScriptZ carScriptZ;
private void Start()
{
// As fallback get it ONCE on runtime
if(!carScriptZ) carScriptZ = GameObject.Find("Spawner").GetComponent<CarScriptZ>();
}
private void Update()
{
float test = carScriptZ.mult;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.