- Home /
Quick question about global variables. Two ways. Are they different?
Just a quick question:
Are A and B scripts exactly the same? I'll be able to work with them as a global variable, without affecting other gameObjects with the same script and etc? Are they just two ways of programming exactly the same thing?
A)
private var lifeScript;
function Start () { lifeScript = gameObject.FindWithTag("Player").GetComponent(life); }
function Update () { var playerLife : life = lifeScript; playerLife.life -= 10; }
and B)
private var lifeScript;
function Start () { lifeScript = gameObject.FindWithTag("Player").GetComponent(life); }
function Update () { lifeScript.life -= 10; }
Thanks.
Answer by Jessy · Dec 26, 2010 at 02:16 AM
They are not "the same". You are creating another variable in the first example. But both of these variables hold reference types, not value types. That means that when you change "lifeScript.life", or "playerLife.life", yes, you will be doing the same thing. So, might as well use the second example, considering playerLife isn't adding anything new.
Here is more information on value vs. reference types.
Also, you should name your classes with an uppercase letter first. i.e. "Life", not "life". And use GameObject.FindWithTag, not gameObject.FindWithTag; using a specific game object, with the latter method, is unnecessary and misleading.
Thanks for your answer, I changed what you said.
What's the difference between "life" and "Life" as a class name? Just aesthetic? I'm not a programmer, so I don't know about program$$anonymous$$g formalizations. Also, using "GameObject.FindWithTag" ins$$anonymous$$d of "gameObject.FindWithTag" changes something in functionality?
thanks
Using capital letters to start the names of classes is a convention that both Unity and $$anonymous$$ono use. Considering you're using both around here, it makes sense to do it for your own classes. :-)
GameObject vs. gameObject doesn't change the result, but the latter method is doing something different that probably isn't as efficient, and definitely isn't as clear. FindWithTag is a method of the class "GameObject", not a method that requires a specific Game Object to work. Using the lowercase "gameObject" works, because you're using the gameObject that the script is attached to, which is an insta
nce of the class, and so can use methods from the class, but to suggest that you are actually using the game object itself in some helpful way, like I said, is misleading. It's not a problem with your code above, but not understanding the concept can lead to problems in the future, so you should practice using it appropriately. Here is an example where relevant confusion happened:
http://answers.unity3d.com/questions/2124/accessing-non-static-members-in-a-static-function-argument
Thanks again! Very informative. I'll from here on pay more attention to these important details!
Your answer
![](https://koobas.hobune.stream/wayback/20220613092541im_/https://answers.unity.com/themes/thub/images/avi.jpg)