Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 program$$anonymous$$g 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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Prefab's Script affecting all the Prefab 1 Answer

Is there any way to edit variables inside other scripts without declaring them as static? 1 Answer

Should I create local copy of global variable? 3 Answers

How to use same code at multiple scenes ? 1 Answer

Global Varible Problem 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges