简体   繁体   中英

Script Reference in Unity

I'm creating a game that have multiples references to scripts, one to another.

For exemple:

  1. Controller script
  2. SomeBehavior script
  3. GUI script

My doubt enter when I can do two different things:

  1. In SomeBehavior access/edit GUI variable/function that I whant... Like:

    1.1. variable = GameObject.Find("GuiTag").GetComponent< GUI script >();
    variable.score.... and go on...

  2. In SomeBehavior access/edit GUI variable/function by Controller.gui (gui a public variable on controller script)... Centralization all the base scripts to one, in that way, having less variables in the scripts, less memory (perraps)...

    2.1. variable = GameObject.Find("ControllerTag").GetComponent< Controller script >();
    variable.gui.score....

Which one is better?

In the Scripts that you will only have one of you can set a public static variable in that class with a public get and private set. On the Start or Awake function you set that variable to this and then you can access that script easily from any other scripts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM