Once played the game it will automatically go to next leverl

Hi I did the same as the tutorial did (please see the code on the bottom part) but when I hit play it will automatically go to the next level

using UnityEngine;

using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour

{

void OnCollisionEnter(Collision other)

{

    switch(other.gameObject.tag)

    {

        case "Friendly":

        Debug.Log("Friendly");

        break;

       

        case "Finish":

        LandingPad();

        break;

        case "Fuel":

        Debug.Log("Fuel Hit");

        break;

        default:

        ReloadLevel();

        break;

    }

   

    void ReloadLevel()

    {

        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

        SceneManager.LoadScene(currentSceneIndex);

    }

    void LandingPad()

    {

        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;

        SceneManager.LoadScene(currentSceneIndex+1);

    }

}      

}

1 Like

Hi Jeffry,

Your code looks fine to me, so the problem is very likely in the scene. Check the environment of the rocket. Is the “Friendly” tag assigned to the launch pad? Double-check the spelling in both your code and in Unity because C# is case-sensitive.

If the launch pad has got the correct tag, check other game objects. You could also try to log other.gameObject.name into your console to see with which game objects your rocket collides on start.

Did this help you fix the issue?


See also:

1 Like

Hello Nina,

I did some check up on my scenes, and I found that there were 2 “Friendly” tags assigned. The issue is ok now.

Thanks

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms