Search Unity

Levels 2d

Discussion in 'Getting Started' started by Yegor42069, Feb 24, 2023.

  1. Yegor42069

    Yegor42069

    Joined:
    Dec 5, 2022
    Posts:
    26
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Finish : MonoBehaviour
    7. {
    8.  
    9.     private void Start()
    10.     {
    11.        
    12.     }
    13.     private void OnTriggerEnter2D(Collider2D collision)
    14.     {
    15.         if (collision.gameObject.name == "Player")
    16.         {
    17.             CompleteLevel();
    18.         }
    19.     }
    20.     private void CompleteLevel()
    21.     {
    22.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    23.     }
    24. }
    25.  
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class Finish : MonoBehaviour
    6. {
    7.     private void Start()
    8.     {
    9.    
    10.     }
    11.     private void OnTriggerEnter2D(Collider2D collision)
    12.     {
    13.         Debug.Log(collision.gameObject.name); // If you don't put this here, you don't even confirmed that this work
    14.      
    15.         if (collision.gameObject.name == "Player")
    16.         {
    17.             CompleteLevel();
    18.         }
    19.     }
    20.     private void CompleteLevel()
    21.     {
    22.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    23.     }
    24. }
    Debugging works like:
    1. Add console message.
    2. Add more console messages to confirm that data is how you expect.
    3. If not track what happening with more console messages.
    4. 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.
    5. Also could be not meet all conditions to use trigger, full chart I found is like this.
     
    Last edited: Feb 24, 2023
  3. Yegor42069

    Yegor42069

    Joined:
    Dec 5, 2022
    Posts:
    26
    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
     
  4. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    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.
     
  5. Yegor42069

    Yegor42069

    Joined:
    Dec 5, 2022
    Posts:
    26
    Ok ill go through the code and check for speelling
     
  6. Yegor42069

    Yegor42069

    Joined:
    Dec 5, 2022
    Posts:
    26
    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
     
  7. TylerJay-B

    TylerJay-B

    Joined:
    Dec 29, 2018
    Posts:
    18
    you should avoid comparing strings, try comparing type instead.

    Code (CSharp):
    1. if (collision.TryGetComponent(out Player player))
    2. {
    3.      // do stuff
    4. }
     
  8. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    My Bold :D

    You just proved my point mate :D