Trying to get a collider working to when the player walks into it changes level. No compile errors but still not working. I am trying to get this so the player can visit the houses within the village I have created. I am aware of slight code variation for 2D and 3D games so this could be my issue?
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
public class HouseLoader : MonoBehaviour
{
public CompletePlayerController controller;
public string House_Blue_Bottom_Right;
public string House_Blue_Top_Right;
void OnTriggerEnter2D(Collider2D Colider)
{
controller = (CompletePlayerController)transform.GetComponent("CompletePlayerController");
if (Colider.gameObject.CompareTag("Player"))
{
SceneManager.LoadScene("House_Blue_Bottom_Right");
SceneManager.LoadScene("House_Blue_Top_Right");
}
}
}
Right now your code would be loading one scene, then another immediately. You would end up in the âHouse_Blue_Top_Rightâ scene, and the âHouse_Blue_Bottom_Rightâ scene would be skipped.
Are you certain that the trigger is being called? Try using âDebug.Log(âtriggeredâ)â inside that function so you know that the trigger is being activated. Debug.Log will print a line into the console window in unity.
Iâm assuming you have many different houses, each of which goes to a different scene. So what you should do is give each House a script like this:
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneChangeTrigger : MonoBehaviour
{
public string sceneName;
private void OnTriggerEnter2D(Collider2D other)
{
if(other.gameObject.CompareTag("Player"))
{
SceneManager.LoadScene(sceneName);
}
}
}
Put that on the house, and when the gameobject tagged âPlayerâ triggers the collider component, the scene will change to the scene name you put in the inspector for that house.
Make sure the object has a collider with the Trigger checkbox checked.
By house I mean the object with the collider that your player will touch to trigger the scene change.
The inspector is the details panel that appears in Unity when you select something in the editor. Itâs where the list of components are, and you can setup default values for variables there.
Since I gave that script a âpublic string sceneNameâ variable, whenever a gameObject has that script on it, you will be able to enter a string for that variable in the inspector. Most variables will show up in the inspector if you make them public.
For each one of your collider objects that should change the scene, you can add the âSceneChangeTriggerâ component, then in the inspector you enter the name of the scene it should change to.
Thank you for your help, and for your script!
Managed to figure out what was wrong, apparently I needed a rigidbod2d on my level loader and also to make it kinematic.
No problem, to explain that a little bit more (which you may have learned already): To have a collision, both objects must have a collider, and at least one must have a rigidbody (usually the player in this scenario, but it doesnât matter). Setting the rigidbody to kinematic is correct as that makes it so that the object does not use the physics engine to move about, and with the collider as a trigger it wonât physically block anything.
When in doubt, either use breakpoints and the debugger in MonoDevelop or Visual Studio, or put âDebug.Log()â statements everywhere to print things to the console in unity. That way you know exactly where the code failed to execute. In your case, you would have seen that the Trigger function did not get called.
Iâm glad you dug in and figured it out for yourself, nice job! Let me know if you have any more questions about the code.
Itâs just a temporary line for debugging information to the console. You put them anywhere you want to be verify is getting executed. Itâs good to delete them once you donât need them anymore, because they can cause lag in the editor if youâre logging too many things in quick succession.
So you could do this to find out what code is being reached in your function:
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneChangeTrigger : MonoBehaviour
{
public string sceneName;
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("SceneChangeTrigger has been triggered!");
if(other.gameObject.CompareTag("Player"))
{
Debug.Log("It was the player!");
Debug.Log("Changing scene to: " + sceneName);
SceneManager.LoadScene(sceneName);
}
}
}
If everything is working correctly you would see the console output:
âSceneChangeTrigger has been triggered!â
âIt was the player!â
âChanging scene to: Scene_House_Blue_03â (or whatever your scene name is)
But if you donât see one of those lines, you know where to look for problems.