- Home /
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)
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
What moghes said, also what is the name of the script above?
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!
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
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
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
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?
If you need more info on static/instance you could check this one: http://unitygems.com/memorymanagement/
Your answer
Follow this Question
Related Questions
NullRerenceException On comparisons 0 Answers
What's the deal with the ..cctor() error? 2 Answers
Array of custom properties? (C#) 1 Answer