• 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 Jake · Aug 05, 2010 at 05:14 PM · global

Global variables!

I cannot, for the life of me, get my variable recognized in another script. Please don't say just read this, because I have. Here is my code.

static var population = 120; static var happiness = 60.0; static var food = 500;

function Update () { if (population > food) happiness -= 0.01 * Time.deltaTime; }

And my second script,

var farms = 4; var harvest = farms * 10;

function Update () { food = food += harvest * Time.deltaTime;

}

EDIT:

When they are static my variables don't even show up,

They work just fine when I have them as normal variables though.

Sorry, I'm new to scripting and am kind of terrible at it. Trying to get better though.

EDIT: Alright I now have it showing up but the second script doesn't work now! And I have no errors! Please help. Here's the code.

var population = 100; static var food = 500; var foodvalue = food; var happiness = 60.0;

function Update () { if (population > food) happiness -= 0.01 * Time.deltaTime; }

and the second script:

var farms = 4; var harvest = farms * 10;

 function Update (){
      PopulationScript.food += harvest * Time.deltaTime;

}

Comment
Add comment · Show 3
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 Jake · Aug 05, 2010 at 07:22 PM 0
Share

I found the answer here, http://answers.unity3d.com/questions/10667/static-variables-problem.

avatar image Wolfram · Aug 06, 2010 at 03:07 PM 1
Share

Use a Singleton ins$$anonymous$$d of static vars (search the forums for that). Declare food as 500.0, to make it a float value.

avatar image cregox · Sep 04, 2013 at 12:23 AM 0
Share

I agree with Wolfram. And here's more about singleton: http://answers.unity3d.com/questions/50716/how-to-make-a-global-variable-in-unity.html#answer-530063

5 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Noztradamuz · Feb 20, 2014 at 04:52 PM

Wow... reading this im just wondering... are you the dev behind Banished?

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 psychentist · Feb 18, 2012 at 02:01 AM

Static variables will not show up in the inspector. If you need to set up a global from the inspector, you will need a variable to set.

 var FoodStatSetting : int;
 static var GlobalFoodStat : int;
 
 function Awake(){
 GlobalFoodStat = FoodStatSetting;
 }

// now you can access "GlobalFoodStat" as a static, and you can set it by changing the FoodStatSetting in the inspector and restarting the test run. You could also move the line of code into Update if you don't want to restart each time.

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
2

Answer by Wolfram · Aug 05, 2010 at 05:48 PM

At the bottom of the page you said you have read is explained what you need to do.

function Update () { PopulationScript.food += harvest * Time.deltaTime;

}

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
1

Answer by cncguy · Aug 05, 2010 at 05:38 PM

OtherScript.food += harvest*Time.deltaTime; 
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 equalsequals · Aug 05, 2010 at 05:18 PM

Not sure I understand exactly but if I have this right:

If your static vars reside in Class1 and you are trying to access them in Class2 then you must refer to this variables as Class1.staticVariable

Hope that helps.

==

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

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

3 People are following this question.

avatar image avatar image avatar image

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