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
0
Question by ungalyant2 · Feb 07, 2012 at 02:16 AM · variablesstaticglobalpublish

Static variable issue in published

Hey guys, so I can manipulate static variables in a class that holds all my between scene information (such as which controller is controlling which unit etc.) However, this functionality only exists when play the game in development, as soon as I publish it, it seems to no longer work, as soon as I get to a line of code that tries to manipulate the static variables, I.e.:

 stats.Controllers[0] = 1;

(Here I'm accessing the class which has the static variable, and adjusting it so controller[0] is searching for input from input device 1.)

The code just gets ignored for the rest of the section, if I have lines after it they are not called at all.

Does anyone have a possible 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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Owen-Reynolds · Feb 07, 2012 at 03:06 AM

I'm guessing the same way that Unity doesn't show static vars in the Inspector, it doesn't like to save them, either. Probably Controllers[0] is giving you a null, since Controllers wasn't created.

Instead of static, you can make them normal public vars, and put the stats script on an empty (which Instantiates them.) You can now set them in the Inspector, they get saved the usual way, etc... . To use them: GameObject.Find("statObject").GetComponent<stats>().Controllers[0]. Or the usual where you grab a reference:

 stats S; // global
 S=GameObject.Find("statObject").GetComponent<stats>(); // in Start
 S.Controllers[0]... // to use

Technically, Patterns people would probably call that a Singleton.

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 ungalyant2 · Feb 07, 2012 at 03:40 AM 0
Share

I certainly understand what you mean, the only issue is how to get it to persist between scenes and be accessible in each, how could this be achieved?

avatar image Owen-Reynolds · Feb 07, 2012 at 06:18 PM 1
Share

There's a function in Object DontDestroyOnLoad(transform.gameObject); which tells something to persist between Scenes. It's pretty much made for this. Hard to find -- think it's in the HowToLoadLevels part of the manual.

avatar image ungalyant2 · Feb 07, 2012 at 09:39 PM 0
Share

still doesn't work, changed it all to what you said and it does nothing in published version, but works fine in normal version.

the code looks like this:

         if (Input.GetButtonDown(slotButton + slot1Cont) && Time.time > inputTimer1)
         {        
             if (slot1Cont != null)
                 stats.Controllers[0] = int.Parse(slot1Cont);
             if (slot2Cont != null)
                 stats.Controllers[1] = int.Parse(slot2Cont);
             if (slot3Cont != null)
                 stats.Controllers[2] = int.Parse(slot3Cont);
             if (slot4Cont != null)
                 stats.Controllers[3] = int.Parse(slot4Cont);
             
             Application.LoadLevel("Building"); // load the game level.                
         }

If I put the Application.Load level before the block of ifs, it works, if I don't, it doesn't.

avatar image Owen-Reynolds · Feb 08, 2012 at 12:50 AM 0
Share

stats.Controllers is for the version using static. If Controllers is normal (non-static,) then it should cause an error. For example, if hitPoints is in orcScript you can say orc1.hitPoints, orc2.hitPoints but you can't say orcScript.hitPoints (why would you want to? which orc is that?)

If this is in the stats script, just use the var name: Controller. If not, look up the script through the game object (above.)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Global Variables Refuse to Cooperate 1 Answer

Not asigned health script 1 Answer

Changing static variables from another script? 1 Answer

My static variable is not changing 2 Answers

How can I edit my static variable in the editor? 1 Answer


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