So, I need to load a different level when the user has crossed the entire level and collides (or triggers) a certain object. For that I have declared
using UnityEngine; using UnityEngine.SceneManagement; public class PacMan : MonoBehaviour { bool GameEnded = false; public float delay; // Ends the level public void GameOver() { if (GameEnded == false) { GameEnded = true; Invoke("Restart", delay); } } public GameObject LvlCompleteUI; // Restarts the level void Restart() { SceneManager.LoadScene(SceneManager.GetActiveScene().name); } public void LvlComplete() { LvlCompleteUI.SetActive(true); Debug.Log("GameOver"); } } and then calling the function via
public class WinTrigger : MonoBehaviour { // public PacMan pacMan; void OnCollisionEnter(Collision collision) { if (collision.collider.tag == "LvlWon") { FindObjectOfType<PacMan>().LvlComplete(); } // FindObjectOfType<PacMan>().LvlComplete(); // pacMan.LvlComplete(); } } But, this is not working as the collision doesn't seem to be working. Neither does trigger. Any ideas why this is not working? Maybe the way I am doing it is wrong.
Edit:
- PacMan is my PACkage MANager; basically it loads the scenes.
- The WinBox is the object that will load the next scene on collision.
- I did have Is Trigger on when I was trying the OnTriggerEnter() method.
PacMan script:
using UnityEngine; using UnityEngine.SceneManagement; public class PacMan : MonoBehaviour { bool GameEnded = false; public float delay; // Ends the level public void GameOver() { if (GameEnded == false) { GameEnded = true; Invoke("Restart", delay); } } public GameObject LvlCompleteUI; // Restarts the level void Restart() { SceneManager.LoadScene(SceneManager.GetActiveScene().name); } public void LvlComplete() { LvlCompleteUI.SetActive(true); Debug.Log("GameOver"); } } Movement Script:
using UnityEngine; public class Movement : MonoBehaviour { public Rigidbody RB; // public bool forward; // public bool back; public bool left; public bool right; // public string isTrueF; // public string isTrueB; public string isTrueL; public string isTrueR; void Update() { // bool forward = Input.GetKey(KeyCode.W); // bool back = Input.GetKey(aKeyCode.S); bool left = Input.GetKey(KeyCode.A); bool right = Input.GetKey(KeyCode.D); // if (forward == true) // { // isTrueF = "True"; // } else { // isTrueF = "False"; // } // if (back == true) // { // isTrueB = "True"; // } else { // isTrueB = "False"; // } if (left == true) { isTrueL = "True"; } else { isTrueL = "False"; } if (right == true) { isTrueR = "True"; } else { isTrueR = "False"; } } void FixedUpdate() { // if (isTrueF == "True") // { // RB.AddForce(1000 * Time.deltaTime, 0, 0); // } // if (isTrueB == "True") // { // RB.AddForce(-1000 * Time.deltaTime, 0, 0); // } RB.AddForce(0, 0, 500 * Time.deltaTime); if (isTrueL == "True") { RB.AddForce(-25 * Time.deltaTime, 0, 0, ForceMode.VelocityChange); } if (isTrueR == "True") { RB.AddForce(25 * Time.deltaTime, 0, 0, ForceMode.VelocityChange); } if (RB.position.y <= 0f) { FindObjectOfType<PacMan>().GameOver(); } // if (Input.GetKey("d")) // { // RB.AddForce(1000 * Time.deltaTime, 0, 0); // } // if (Input.GetKey("a")) // { // RB.AddForce(-1000 * Time.deltaTime, 0, 0); // } // if (Input.GetKey("w")) // { // RB.AddForce(0, 0, 1000 * Time.deltaTime); // } // if (Input.GetKey("s")) // { // RB.AddForce(0, 0, -1000 * Time.deltaTime); // } } } Win Trigger Script:
using UnityEngine; public class WinTrigger : MonoBehaviour { // public PacMan pacMan; void OnCollisionEnter(Collision collision) { if (collision.collider.tag == "Finish") { FindObjectOfType<PacMan>().LvlComplete(); } // FindObjectOfType<PacMan>().LvlComplete(); // pacMan.LvlComplete(); } } Player Collision Script:
using UnityEngine; public class PlayerCollision : MonoBehaviour { void OnCollisionEnter (Collision collisionInfo) { if (collisionInfo.collider.tag == "Obstacles") { GetComponent<Movement>().enabled = false; FindObjectOfType<PacMan>().GameOver(); } } Here's the hierarchy and first map:






OnCOllisonEnter. \$\endgroup\$