- Home /
Have objects persist in one scene, and not across all scenes? (C#)
Hey everyone Jaicrimson here.
I've been trying to figure this out for a while now, and I've come across some really close answers but in the end no cigar.
I am making an rpg, and I would like my player to persist in just one scene. Basically, you run around, bump into an enemy, and a battle starts (like pokemon and trainers or wild grass). The battle takes place through another scene, and after the battle is over, the first scene is reloaded.
Which is the problem.
When I reload the first scene, all positions are reset. The player is spawned back at the starting point, even after having made it through half the level.
I've tried a bunch of different things for this, but I can't even figure out how to turn the players position on the map into a variable/reference. I also can't even get the player object to NOT persist throughout both scene without destroying it.
Right now I have
void OnTriggerEnter(Collider other){
if (other.transform.tag == "Battle"){
nPvt.SetActive(false);
Application.LoadLevel (1);
which disables the object before the battle scene starts. nPvt is a reference variable to the player's object, I was reading that setting a reference like this could bypass the inspector not being able to find the object after its disabled.
this is the reference variable code.
void Awake (){
nPvt = GameObject.Find("newPivot");
And even if I were able to re-enable this object with
void OnLevelWasLoaded(){
if(nPvt == isActiveAndEnabled){
Destroy(GameObject.Find ("newPivot"));
nPvt.SetActive(true);
print ("Jet fuel cant melt steel beams");
(disclaimer: I use Destroy because when the level loads, another player object spawns at the start and I destroy that new instance before trying to re-enable the last instance. Not sure how appropriate that is, though.)
then I have no idea if it will even re-enable the object in it's last known position or if it will just respawn it.
I've gotten myself into a bit of a mess here, can any one help out?