- Home /
Load Scene Script doesn't work anymore
Hello everybody,
my script, which worked a few weeks ago in another Project, doesn't work anymore and I don't know why.
My Problem: I want my Player Object to load a scene when the Player collides with a collider2D. In addition I want my Player Object in the other scene (which should be loaded) to load on a certain point (which can be added in the inspector). Although the scene loads, the Player Object doesn't load there where I want it to load.
My code is below as a comment. If anyone knows what is wrong please tell me. There are no error messages, except that Application.LoadLevel is obsolete. But I don't want to change that.
Thanks in advance.
P.S.: There are a LOAD of loads in this question. :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Script Name : $$anonymous$$onoBehaviour {
public string sceneName;
public float xPosition;
public float yPosition;
void OnCollisionEnter2D(Collision2D col) {
if (col.transform.tag == "Player") {
col.transform.position = new Vector3 (xPosition, yPosition);
Application.LoadLevel (sceneName);
}
}
}
So, the scene is loaded but the player is not placed at the specified location?
Answer by efeguclu · Aug 21, 2017 at 11:46 AM
use DontDestroyOnLoad() method in awake;
void Awake(){
DontDestroyOnLoad(gameObject);
}
Thanks for your help. I tried this method days ago but it didn't work. When I attach it to the "Collision-Wall-Load-Scene-Object", it appears in the other scene at the same point. If I attach it to my Player, my Player Object got stuck in a spawning loop. That's funny, but not helpful.
If you have any other suggestions I would be very grateful.
Thanks.
It needs to be attached to your player - the thing that you want to retain its position between scenes. And you also need to make sure that that object doesn't exist in the scene hierarchy of the new level you're loading.
Thank you very, very much. That makes sense. If there are two player objects both get loaded and so I can avoid the infinite spawn loope. Thanks!
Your answer
Follow this Question
Related Questions
Application.LoadLevel(Application.LoadedLevel) causes freeze 0 Answers
Script looses attached AudioSource when level is restarted 1 Answer
Application.LoadLevel() changes lighting for some reason? 12 Answers
How to choose coordinates on Application.loadlevel 2 Answers
How to load Level using scene object?? 2 Answers