- Home /
can gameobjects be stored as static?
The original problem involves real time strategy elements and saving objects in a scene before leaving. I need to be able to either save the state of the scene or save everything in the scene as is. My solution is to have a script called gamedatabase and basically override application.loadlevel() to a function within this script that saves everything before loading the level.
It only breaks when reloading the scene. Since the objects are static, I can't check them as I'm loading other scenes. It prints each name as it is stored so I know that is performing properly. I think the objects might be lost though as it leaves the scene if it is running on pass by reference. This is tied to an object so that the start function is called. Should I not be tying to anything? What would help this?
static var built : GameObject[] = new GameObject[50];
static var positions : Vector3[] = new Vector3[50];
static var v : int = 0;
function Start ()
{
print("gamedatabase start called");
//if(EditorApplication.currentScene == gamedatabase.groundscene)
//{
print("loading objects");
//destroy anything not terrain
if(!gamedatabase.starting)
{
//var objects : GameObject[] = GameObject.FindObjectsOfType(typeof(MonoBehaviour)) as GameObject[];
var objects : GameObject[] = GameObject.FindObjectsOfType(typeof(GameObject));
for(var tobedestroyed : GameObject in objects)
{
if(tobedestroyed.tag != "terrain" && tobedestroyed.tag != "MainCamera")
{
print("removing " + tobedestroyed.name);
//Destroy(tobedestroyed);
}
}
for(var x : int = 0; x < v; x++)
{
//add objects
//print("attempting to add " + built[x].name);
//--------------------------------it can't seem to add the objects; it says the objects have been destroyed
GameObject.Instantiate( built[x], positions[x], Quaternion.identity);
}
}
gamedatabase.starting = false;
//}
}
static function leavescene(toscene)
{
print("leave database called");
print("current scene is " + EditorApplication.currentScene);
//if(EditorApplication.currentScene == groundscene)
//{
var objects = GameObject.FindObjectsOfType(typeof(GameObject));
v = 0;
print("storing objects");
var temp : GameObject;
var temp2 : GameObject[] = new GameObject[50];
for(var tobestored : GameObject in objects)
{
//print(tobestored.name);
if(tobestored.tag != "terrain")
{
built[v] = tobestored;
positions[v] = tobestored.transform.position;
print(tobestored.name + " stored");
v++;
}
}
//}
Application.LoadLevel(toscene);
}
Answer by iwaldrop · May 04, 2013 at 01:16 AM
Sure they can. But when you load a new scene your script probably has a new instance of itself created.
A good way to get around this is to actually not use a static method/variable, attach the 'persistance' script to a gameObject, and set that object not to be destroyed between scene loads.
function Awake ()
{
DontDestroyOnLoad(transform.gameObject);
}
I read the link, Does that mean that the objects from this scene will be in the next scene you load too? Oh and how to use a persistence script?
By 'persistance script' I simply meant whatever script you use to call the above method. You use it like any other $$anonymous$$onoBehaviour. Say, for instance, you wanted a script that only kept a GO from being destroyed between scene loads. You might call it PreventDestructionBetweenScenes, and have only the Awake function as demonstrated above in it with the addition that it would remove itself so that it doesn't keep calling DontDestroyOnLoad every time you load a scene (because that would just be wasteful), like:
function Awake ()
{
DontDestroyOnLoad(transform.gameObject);
Destroy(this);
}