- Home /
How to change scenes without reloading them.
Hey guys! I am making an open-world game and I am running into a small issue. My player and all things associated with him are static and DontDestroyOnLoad, that's not the issue. My issue is that objects (such as weapons) are respawning when I exit the building scene and into the exterior scene. How do I prevent this?
Any help would be greatly appreciated! Thanks!
Here is the general code I am using:
void Update()
{
if(Input.GetKeyUp(KeyCode.E))
{
Application.LoadLevel(1);
}
}
Answer by Loius · May 02, 2013 at 05:25 AM
Save the state of the scene and on scene load destroy objects that should be gone based on that save.
PlayerPrefs is probably the hassle-free-est way to save scene state. At the very simplest each object would store its name in PlayerPrefs if it wants to suicide next time the level loads.
There's some serializer by whydoidoit that does stuff you might want: http://whydoidoit.com/unityserializer/
Answer by Fornoreason1000 · May 02, 2013 at 05:32 AM
you could make a "temp" class that handles any recent events such as the weapon you picked up, the enemies you killed. if the player has picked up those weapon or killed those enemies, you can make a temp file saying so, so when you exit and re-enter the dead bodies disappear and the weapons don't re spawn. you can clear this temp or cache after a certain amount of levels to prevent it from building to massive size but only keep the last 3 or so level you were in. the thing about this method, if you shutdown the game , whether you save or not, the temp will be recreated, so all the weapons and enemies re spawn. this is actually a good thing in some cases to stop players from getting bored.
you might want to make the Temp class static... you don't want to create new instances of temp on each object, it really depends on your scripting heirachy. when you load a level Temp should check for the state of the scene you left it in, and set the states for the objects, e.g Enemy A killed at 2,0,0 and it was kill them and place them there. weapons it will destroy ones you already picked up. etc.
another option is to save the data via PlayerPrefs, but I wouldn't advise this for open world games since the large save file you may end up with. but you may do so, for once only pick ups or scripted enemies.
Your answer
Follow this Question
Related Questions
Problem with draging object to scene 2 Answers
What are GameTiles? 1 Answer
Change Object's Origin 1 Answer
Select object in another scene 1 Answer
Persistent game objects 1 Answer