Not Working properly. Application.LoadLevel

My player has the tag of FPSController, but when I collide with the plane that has that script, the level does not change, in build settings, the next level is 2

void OnTriggerEnter(Collider other) {
	if (other.gameObject.CompareTag("FPSController")) {
		Application.LoadLevel (2);
	}
}

Current Code:

public class ToLevelTwo : MonoBehaviour {

private string[] scenePaths;

void OnTriggerEnter(Collider other) {

	if (other.gameObject.CompareTag("Finish")) {

	SceneManager.LoadScene(2);

Try explicitly comparing tag. So.. If(other.gameObject.tag == "Finish")

–

Current Code: public class ToLevelTwo : MonoBehaviour { private string[] scenePaths; void OnTriggerEnter(Collider other) { if (other.gameObject.CompareTag("Finish")) { SceneManager.LoadScene(2); } } }

–

6 Answers

6

Application.LoadLevel has been deprecated. You should be using SceneManager instead. Specifically, LoadScene

You can also use the override that takes a string to provide the name of a scene instead of the scene index, but that’s really up to you.

So should i not use application? when i replace LoadLevel with LoadScene, it makes the error: 'UnityEngine.Application' does not contain a definition for 'LoadScene'

–

i added. using UnityEngine.SceneManagement; and it stopped the errors, but still it wont send me to scene 2

–

No problem, if you need anymore help don't hesitate to ask :)

–

Current Code:

public class ToLevelTwo : MonoBehaviour {

private string scenePaths;

void OnTriggerEnter(Collider other) {

if (other.gameObject.CompareTag("Finish")) {

SceneManager.LoadScene(2);
	}
}

}

Try explicitly comparing tag. So…

If(other.gameObject.tag == “Finish”)

Make sure to Add both scene to Build on the Build settings

I had similar issues. Add a debug.log statement inside your OnTriggerEnter function and have it output a text message to the log. It doesn’t matter what it says, just trying to determine if it’s a loadscene issue, or if it’s a triggering issue. Each would have different possible causes. Let us know whether or not the debug message shows up. :slight_smile:

Stupid me, i forgot to make the plane’s collider “Is trigger”. My final script was:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

public class ToLevelTwo : MonoBehaviour {

private string scenePaths;

// Use this for initialization

void OnTriggerEnter(Collider other) {

	if (other.gameObject.CompareTag("Finish")) {

		SceneManager.LoadScene("Term Game 1");
	}
}

}

Glad to hear it's working for you. We've all been there... myself included. :) And thanks for posting your fix... this may be helpful to others.

–