GameObject,并将它命名为“传感器”。然后,我向传感器添加了一个BoxCollider和一个脚本(script_sensor)。GameObject,并将其命名为“出租车”。我在出租车中添加了一个脚本(script_taxi),并在脚本中创建了一个公共Collider2D。GameObject分配给出租车的公共Collider2D。script_taxi访问script_sensor。基本上,如何从script_taxi (框2)访问script_sensor (图像上的框1)?
Ps:当我垃圾出租车预制,每个传感器对象应该能够找到它自己的容器游戏对象(出租车)。
发布于 2017-07-28 19:06:41
如果要从script_taxi访问script_sensor,则需要对script_sensor中的script_taxi进行某种类型的引用。然后,您可以使用GetComponent。您也可以使用GameObject.Find的一个变体,但是这些调用可能很昂贵。
public class script_sensor : MonoBehavior {
[SerializeField]
private GameObject taxi;
private script_taxi taxiScript;
void Start() {
taxiScript = taxi.GetComponent<script_taxi>();
}
}此外,还建议遵循C#命名约定。
script_sensor => Sensor
script_taxi => Taxi
public Collider2D col; => [SerializeField] private Collider2D col;
发布于 2017-07-28 19:25:55
或者用GameObject.Find()找到这样的出租车GameObject:
script_taxi taxi;
void Awake()
{
taxi = GameObject.FindWith("taxi").GetComponent<script_taxi>();
}https://stackoverflow.com/questions/45380576
复制相似问题