1
votes

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?

1
Benchmark and test. You should store results of GameObject.Find if you'll use them again, Find is a spendy call. As far as memory goes... you should probably read up on C#'s reference types. "Less variables in the scripts" has little-to-no relation to memory. - Jerdak

1 Answers

0
votes

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.