Accessing variable from other script

前端 未结 2 1258
灰色年华
灰色年华 2020-12-21 15:36

How do I call a variable from other script components?

mirror.transform.position = cop.GetComponent(\"AI_Car\").mirrorPos;

It seems that th

2条回答
  •  情歌与酒
    2020-12-21 16:38

    You can cast it to the right type:

    mirror.transform.position = ((AI_Car)cop.GetComponent("AI_Car")).mirrorPos;
    mirror.transform.position = cop.GetComponent().mirrorPos;
    

    Anyway, the best is to make an AI_Car property, then get it on start, so you can simply read it anywhere in the class by aiCar.mirrorPos or similar.

提交回复
热议问题