- Home /
Application.LoadLevel sometimes loads the wrong scene
So I have a project that has 4 Levels. If the player walks up-to an object and enters its collider, it asks the user to "Press - F". If the user presses F, they are taken to a hub where they can move around and, using the same process, select the next level they wish to enter. (Very reminiscent of Croc or Sly 1)
It was working absolutely fine. I wrote up a script that writes the name of the last levels exit point, so that the player will spawn at the right position close to the previous level in the hub and that works (I can see the names it writes to .txt and the player indeed spawns at the spawn-point with the same name, perfect)
But now, all of a sudden, it sometimes (80% of the time) WILL NOT load one of the levels.
I have 4 levels. 1) Start screen 2) Hub 3) Forrest 4) Lava
At start screen, pressing any key loads the Forrest level. The player can exit the Forrest level and return to the Hub level. The player can then chose to either go back to the Forrest level, or to go to the Lava level. But when the player presses F to load the Lava level, it simply loads the Forest level again.
The only time this isn't true, is when I load the Lava level in the inspector and start there instead of the Forest. Then for some reason It will work fine again, even if I start from any other level... But it breaks again the moment I save or export.
There's a video I made yesterday. IT WAS WORKING FINE.
The only thing I've changed in my code is from this code:
System.IO.File.WriteAllText("C:/Users/Ecto/Desktop/GAME DEV/Drake/DragonGame/Assets/Resources/SpawnPoint.txt", HubSpawnPoint);
to this code:
System.IO.File.WriteAllText(Application.dataPath + "/Resources/SpawnPoint.text", HubSpawnPoint);
Purely so that It will work when I export the level, but even If I change the code back to how it was, this glitch still happens.
PLEASE any help would be great. I hope that its just a stupid mistake that I missed.
Sometimes I get these 2 errors:
m_InstanceID == 0
PPtr cast failed when dereferencing! Casting from PlayerSettings to ProceduralMaterial!,
EDIT
HAHA!
Upon reading through the pictures I posted it seems that somewhere along the line I deleted the code that declares the collsion box! I put the code back and it seems tow work. Strange that it didnt throw a null as the box would have been undefined.
EDIT 2
The exported version still has the glitch.
EDIT 3
Changed the scripts so that nothing is public. Same problem. Im being told that sometimes Unity can skrew up its assets, hence the "m_InstanceID == 0" error I sometimes get... So I might have to start from scratch.
EDIT 4
Re-added all the scenes and even remade the scene in question from scratch. Same issue is still there. It works sometimes while other times it just does not.
using UnityEngine;
using System.Collections;
public class LoadLevel: MonoBehaviour
{
public string LevelToLoad;
public string HubSpawnPoint;
public string GuiText;
public GameObject LoadArea;
public bool CanLoad;
void Start ()
{
CanLoad = false;
}
void OnTriggerEnter(Collider LoadArea)
{
if (LoadArea.tag == "Player")
{
CanLoad = true;
}
}
void OnTriggerExit(Collider LoadArea)
{
CanLoad = false;
}
void Update ()
{
if (Input.GetKeyDown("f") || Input.GetButtonDown("Fire1") && CanLoad == true)
{
System.IO.File.WriteAllText(Application.dataPath + "/Resources/SpawnPoint.text", HubSpawnPoint);
Application.LoadLevel(LevelToLoad);
}
}
void OnGUI()
{
if (CanLoad == true)
{
GUI.Label(new Rect((Screen.width /2), (Screen.height /2), 100, 100), ""+GuiText);
}
}
}
$$anonymous$$aybe you can post a screenshot of the build window to have a look at the scenes values you gave, and the different parts of code where you load the scenes.
Im simply using Application.LoadLevel(LevelToLoad); to load the level, where LevelToLoad is just a public string that is the same as the level name (And it is the same, its the first thing I checked) http://puu.sh/8tiYt.png
So if LevelToLoad is public, it means that another script modify its value. Your problem may be in the others scripts that access this variable.
I was afraid that this would be the problem. Ive had trouble with it before when handling multiple instances of the same interactive object. Will change that and see how it comes along
This seems to work. Will test and see if it breaks.
Thanks for the help
Edit Still broken...