Im making a a platformer style game and im trying to add levels im pretty sure i wrote the code correctly but nothing is working and I dont know why here is my code Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class Finish : MonoBehaviour { private void Start() { } private void OnTriggerEnter2D(Collider2D collision) { if (collision.gameObject.name == "Player") { CompleteLevel(); } } private void CompleteLevel() { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); } }
Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class Finish : MonoBehaviour { private void Start() { } private void OnTriggerEnter2D(Collider2D collision) { Debug.Log(collision.gameObject.name); // If you don't put this here, you don't even confirmed that this work if (collision.gameObject.name == "Player") { CompleteLevel(); } } private void CompleteLevel() { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); } } Debugging works like: Add console message. Add more console messages to confirm that data is how you expect. If not track what happening with more console messages. In above example if you don't get console message at all this mean you don't mark trigger option in Inspector or maybe there is no Collider2D at all. Also could be not meet all conditions to use trigger, full chart I found is like this.
I dont have any console messages or errors popping up it just doesn't work I checked if I added a trigger and I added all my colliders and everything was there I just dont Understand why its not working
I hate to be pedantic but when you're typing in code by hand, watch your spelling/grammar, it's VERY sensitive, the slightest typo and it won't work.
So i fixed my indentation and i checked my spelling errors and i had none still doesnt work I dont know why i have been going over this for like 2 days now
you should avoid comparing strings, try comparing type instead. Code (CSharp): if (collision.TryGetComponent(out Player player)) { // do stuff }