简体   繁体   English

unity/c#:来自另一个游戏对象的数据

[英]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.

相关问题 Unity C#GameObject与另一个GameObject发生冲突 - Unity C# GameObject collides with another GameObject Unity C#从同一GameObject上的另一个脚本获取变量 - Unity C# getting a variable from another script on the same GameObject Unity 5.6-C#-使用AddComponent将游戏对象添加到另一个游戏对象<T> - Unity 5.6 - C# - Adding a GameObject to another GameObject with AddComponent<T> 如何在 Unity3d 中将 C# 组件从一个游戏对象传递到另一个游戏对象 - how can I pass a C# component from one gameobject to another in Unity3d 尝试将 GameObject 分配给另一个脚本中的变量(Unity C#) - Trying to assign GameObject to variable in another script (Unity C#) 如何在 Unity c# 中的另一个游戏对象上使用 OnCollisionStay? - How to use OnCollisionStay on another gameObject in Unity c#? Unity2D C#将GameObject随机生成在另一个GameObject上的区域中 - Unity2D C# Randomly Spawn GameObject in an Area over another GameObject 我需要使用 c# 将一个游戏对象旋转到 Unity 中的另一个游戏对象,但我希望旋转仅在 z - I need to rotate a gameObject towards another gameobject in Unity with c#, but i want the rotation to be only in z 在 Unity 中从 ac# 接口事件引用游戏对象 - referencing a gameObject from a c# interface event in Unity Unity C#从GameObject数组获取灯光组件 - Unity C# Getting Light Component from GameObject Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM