• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Al-Anselmo · Dec 26, 2010 at 12:20 AM · variablescriptingbasicsstaticglobal

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Al-Anselmo · Dec 26, 2010 at 02:44 AM 0
Share

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 programming formalizations. Also, using "GameObject.FindWithTag" ins$$anonymous$$d of "gameObject.FindWithTag" changes something in functionality?

thanks

avatar image Jessy · Dec 26, 2010 at 03:22 AM 1
Share

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

avatar image Jessy · Dec 26, 2010 at 03:31 AM 1
Share

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

avatar image Al-Anselmo · Dec 27, 2010 at 01:55 AM 0
Share

Thanks again! Very informative. I'll from here on pay more attention to these important details!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to make a var "global" 0 Answers

Unable to modify a variable in another script 2 Answers

Global Variables Refuse to Cooperate 1 Answer

Increase Static Var gradually 1 Answer

Global variables - static keyword ? UnityScript? javascript? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges