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 pborg · Jun 08, 2013 at 05:25 PM · nullreferenceexceptionstaticclassesaccessing

should class members ever be static?

Hi I'm using a script as a makeshift database called Gametempdatabase. I'm using it to save player names and character roles for when it loads to a separate scene. (no long term saving yet)

 class playerdata
 {
     var playerin : boolean;
     var playername : String;
     var playerrole : String;
     public function playerdetails()//doesn't work either
     {
         print("from database: " + playerin.ToString() + " " + playername + " as a " + playerrole);
     }
 }
 
 //playersdata is not getting separate instances
 static var playersdata : playerdata[] = new playerdata[4];

I changed to static previously but that caused all instances to be saved to the same data. How do I fix this?

Sample writing of the classes:

 //print(Gametempdatabase.builderscene);
     print("size of playersdata array: " + Gametempdatabase.playersdata.length.ToString());
     for(var sc : int = 0; sc < maxplayers; sc++)
     {
         print(playerin[sc].ToString() + " " + playername[sc] + " as a " + playerrole[sc]);
         Gametempdatabase.playersdata[sc].playerin = playerin[sc];
         Gametempdatabase.playersdata[sc].playername = playername[sc];
         Gametempdatabase.playersdata[sc].playerrole = playerrole[sc];
         
         print("database version: " + Gametempdatabase.playersdata[sc].playername + " " + Gametempdatabase.playersdata[sc].playerin.ToString() + " " + Gametempdatabase.playersdata[sc].playerrole);
         //GameObject.Find("setup object").GetComponent("Gametempdatabase").playersdata[sc].playerdetails();
     }
     print("checking for override using loop");
     for(var c : int = 0; c < maxplayers; c++)
     {
         print("database version: " + Gametempdatabase.playersdata[c].playername + " " + Gametempdatabase.playersdata[c].playerin.ToString() + " " + Gametempdatabase.playersdata[c].playerrole);
     }

Once again, the data members of playerdata were static but changed due to all members changing. (now it's giving a null reference exception)

Comment
Add comment · Show 4
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 moghes · Jun 08, 2013 at 06:24 PM 0
Share

what really you want to achieve? you have to have a reason to make them static.. Generally the concept of using static variables and function is misunderstood.

Please be more clear, about when you are calling your function, from where .. being specific will help other user to answer you more specific :) cheers

avatar image DaveA · Jun 08, 2013 at 06:45 PM 0
Share

What moghes said, also what is the name of the script above?

avatar image pborg · Jun 08, 2013 at 07:03 PM 0
Share

The first one is called Gametempdatabase and second one is called ScriptGeneralwetup. The purpose of making it static is to make it consistent across scenes- setup in one scene, build the players in the others, use gametempdatabase to hold them via static variables. The function in the second batch of code is being called immediately before loading another scene of the game.
Thanks!

avatar image pborg · Jun 09, 2013 at 03:55 AM 0
Share

It should also be noted that last for loop in the second batch of code only prints once despite maxplayers being at 4. the others don't show up as empty logs, they're just not being called. It's weird

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Jun 08, 2013 at 07:11 PM

Create an empty game object, store your info without static and use

 DontDestroyOnLoad(gameObject);

http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html

Do it as many times as you need

Comment
Add comment · Show 3 · 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 pborg · Jun 08, 2013 at 07:49 PM 0
Share

While this does prove an alternative solution, I'm really looking at what's wrong with my code. I could reorganize this to have a working solution, I'd like to fix my current solution to help in the long run on other projects as well. The main problem is just using a static array for classes. Thanks for your help though

avatar image fafase · Jun 09, 2013 at 07:29 AM 0
Share

Your problem lies in the principle of static against instance members. An instance member belong to the instance object, each object has its own instance members totally independent from the other members despite sharing the same name

obj1.name != obj2.name

(except if obj1 == obj2...)

Now using static means the variable does not belong to any instance but to the class. That is why you access with the class name:

 ClassName.static$$anonymous$$ember

Then any member of that class uses the same unique static variable.

So any call would overwrite the same static variable and in the end you only save the last assignment.

So to fix your issue, you need an instance of the storage class for each object you wish to save data from or use an array of storage object with a way to identify who is who later, like a name or an index value.

Is this what you meant?

avatar image fafase · Jun 09, 2013 at 07:30 AM 0
Share

If you need more info on static/instance you could check this one: http://unitygems.com/memorymanagement/

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullRerenceException On comparisons 0 Answers

What's the deal with the ..cctor() error? 2 Answers

Array of custom properties? (C#) 1 Answer

NullReference Problem with pragma strict 1 Answer

accessing a variable from another script. 3 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