• 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
0
Question by Daniel 10 · Jul 19, 2010 at 04:28 PM · globalfindobjectoftype

Calling variables in external script - inconsistent results

Ive got a game-state script where I initialize and update variables that are "global".

Game_State.js:

var globalVariable_1 = "hello";  // string 
var globalVariable_2 = 20;  // number 
var globalVariable_3 = false;  // bool 

Other_Script.js:

private var Game_State : scr_Game_State; function Start () { Game_State = FindObjectOfType(scr_Game_State);

Debug.Log("globalVariable_1 = " + Game_State.globalVariable_1); Debug.Log("globalVariable_2 = " + Game_State.globalVariable_2); Debug.Log("globalVariable_3 = " + Game_State.globalVariable_3); }

Output:

globalVariable_1 = hello 
globalVariable_2 = 1 
globalVariable_3 = true 

As you can see, the output is correct only for the string-type, but when initializing numbers or booleans the value retured is incorrect.

Can anybody make sense of this, and hopefully propose a solution?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Tetrad · Jul 19, 2010 at 05:09 PM

Well first of all you can declare your variables static and then you don't have to do the FindObjectOfType nonsense. Just call your variables exactly the same way you have them in your example code.

Second, try declaring the types of your variables. So, like this:

var globalVariable_2 : int = 20;
Comment
Add comment · Show 2 · 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 Mike 3 · Jul 19, 2010 at 05:25 PM 0
Share

No need to set the type for int, bool or string - they're all deter$$anonymous$$ed implicitly by the compiler

avatar image Daniel 10 · Jul 19, 2010 at 05:29 PM 0
Share

That works great!

However, the variables now disappear from the inspector-pane. Not such a big deal - can always set them in the script - but it would be cool to just be able to adjust them as you do with other variables.

avatar image
0

Answer by Mike 3 · Jul 19, 2010 at 05:27 PM

My guess is that you changed the values of your public variables at some point in the script, without realising (or forgetting - heck it happens all the time to me :P) how unity serializes public variables

What happens is that unity will keep on persisting the values which were set in the inspector (which will have been 1 for var2, true for var 3) after the first time they're set, and disregarding script changes for the values after that

If you want to change them, you should change them in the inspector for the script

Note - I still think you should use Tetrad's answer of making them static.

Comment
Add comment · 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
0

Answer by Daniel 10 · Jul 19, 2010 at 09:06 PM

Thanks for your answers :)

Im definitely going with the static variables - works great!

Comment
Add comment · Show 1 · 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 Mike 3 · Jul 19, 2010 at 09:13 PM 0
Share

mark Tetrad's answer as solved then :D (the tick under the vote up/down thing)

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

global variable problem 2 Answers

Global Variables: import script (boo) 2 Answers

Local-Global error. 1 Answer

Camera rapidly changes it's position when the parent changes. 0 Answers

Local Look Rotation 0 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