Home > Enterprise >  How to change scenes on collision with an object with a certain tag
How to change scenes on collision with an object with a certain tag

Time:10-10

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class end : MonoBehaviour
{

   void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.tag == "Player")
    {
        SceneManager.LoadScene("Level2");
    
    }
}
 
}


I have no errors with my code, but it doesn't work. Any suggestions? The object with the code has a rigid body and a box collider, the player object does have the Player tag and also has a rigid body and a box collider, the scene is named "Level2" and is loaded into the build settings, any suggestions will be greatly appreciated.

CodePudding user response:

First, ensure that function is being called at the moment of visual collision. Read the docs to check if any collision condition is complied.

Then, avoid straightforward equality check. Consider using gameObject.CompareTag(“Player”) instead, which is faster and more reliable.

CodePudding user response:

first, make sure your collision is working and the tag is also correct which you are comparing.

Also, make sure you added a scene in the build settings If not, go to File > Build Settings Click on 'Add Open Scene' or drag and drop your all scenes in ' Scenes In Build '.

for more information see docs: https://docs.unity3d.com/Manual/BuildSettings.html

  • Related