Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do I change scene with a keyboard input? This is what I have for the moment.

Discussion in '2D' started by erikdiep555, Feb 5, 2020.

  1. erikdiep555

    erikdiep555

    Joined:
    Oct 12, 2019
    Posts:
    3
    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6.  
    7. public class SceneManegment : MonoBehaviour
    8. {
    9.     public int index;
    10.     public string levelname;
    11.     public KeyCode changeScenesKey;
    12.     bool colliding;
    13.  
    14.     private void OnTriggerEnter(Collider other)
    15.     {
    16.         colliding = true;
    17.     }
    18.  
    19.     private void OnTriggerStay(Collider other)
    20.     {
    21.         colliding = true;
    22.     }
    23.     private void OnTriggerExit(Collider other)
    24.     {
    25.         colliding = false;
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         if (colliding && Input.GetKeyDown(changeScenesKey))
    31.         {
    32.             SceneManager.LoadScene(1);
    33.             Debug.Log(" hi ");
    34.  
    35.         }
    36.     }
    37.  
    38.  
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Code (CSharp):
    1. private void OnTriggerEnter(Collider other)
    2.     {
    3.        if(other.tag == "Player")
    4.         colliding = true;
    5.     }
    I think your code should work as long as your object (I'm assuming is the player) is tagged "Player" and also has a physics2D collider set to IsTrigger. This should return True upon enter and False upon exit. Be sure to add the same line of code into your OnTriggerExit function.
     
  3. erikdiep555

    erikdiep555

    Joined:
    Oct 12, 2019
    Posts:
    3

    Then should I add an another Box Collider2D on the "Player" that is set to IsTrigger?
    I did the changes with the code and my object was already tagged "Player". Still dosen't work :/
    The Debug dosen't detect any trigger when I press any of my key input.
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @erikdiep555

    Usually a good question has an explanation, what you have done and tried, what kind of scene setup you have, where have you attached the components... and so on. You didn't even bother to write a question... You haven't even told where have you attached this script to.

    Also, calling the script "SceneManegment" (with a typo) also doesn't make it clear where this script should be used and what its purpose is - you could rename the script to something like "DoorEnterTrigger" - whatever makes sense for its use.

    "Then should I add an another Box Collider2D on the "Player" that is set to IsTrigger?"

    For me, it makes more sense if the listening object has the trigger - so if your script is attached to a door or portal, it is the one that has the collider set to trigger and OnTriggerEnter method listening for player to enter it, not other way around.

    Also, the following makes me suspect you might have something else setup incorrectly - you ask this question in "2D" area of forum, yet you are using Physics 3D system methods... hopefully you have used Physics system Colliders instead of Physics2D Collider2D's as these are not working together - OnTrigger enter doesn't detect Collider2Ds, only Colliders.
     
    MisterSkitz likes this.
  5. Wezai

    Wezai

    Joined:
    Dec 30, 2016
    Posts:
    74
    Also I would like to add that from what it looks like you don't really need to call OnTriggerStay() since if you didn't trigger OnTriggerExit() it means you are still inside the trigger.
     
    MisterSkitz likes this.
  6. erikdiep555

    erikdiep555

    Joined:
    Oct 12, 2019
    Posts:
    3
    I have never posted here before, sorry for the lack of context.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class SceneManagement : MonoBehaviour
    6. {
    7.     public int index;
    8.     public string levelname;
    9.     public KeyCode changeScenesKey;
    10.     bool colliding;
    11.  
    12.     private void OnTriggerEnter2D(Collider2D other)
    13.     {
    14.         if (other.tag == " Player")
    15.             colliding = true;
    16.     }
    17.  
    18.     /*private void OnTriggerStay2D(Collider2D other)
    19.     {
    20.         if (other.tag == " Player")
    21.             colliding = true;
    22.     } */
    23.     private void OnTriggerExit2D(Collider2D other)
    24.     {
    25.         if (other.tag == " Player")
    26.             colliding = false;
    27.     }
    28.  
    29.     private void Update()
    30.     {
    31.         if (colliding == true && Input.GetKeyDown(changeScenesKey))
    32.         {
    33.             SceneManager.LoadScene(1);
    34.             Debug.Log(" hi ");
    35.  
    36.         }
    37.     }
    38.  
    39. }
    I have changed to 2D now. Still nothing have changed, I have done all of the above I think. Would you like to see a picture on how I have organize my gameobject and where I have put all of my scripts?
     
    Last edited: Feb 6, 2020
  7. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  8. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Your script is a MonoBehavior which means it must be attached to a game object in your scene (Hierarchy). In this instance you would want to either attach the script to your scene change object or the player itself as @eses mentioned. I also like his advice he gave that the IsTrigger collider needs to be the collider the player will run into. ( Physics2D collider) because it makes more logical sense usually. Your script will not work unless a few requirements are met here.
    1. Your Player object must have a TAG named "Player". (Punctuation matters!)
    2. Your script must be attached to every GameObject you want this script to work for.
    3. If you're 2D game developing, make sure that you're using 2D physics such as OnTriggerEnter2D, BoxCollider2D, etc...
    4. The scene you wish to travel to must exist within the BuildSettings in your project: File>BuildSettings>Add Open Scenes
    As long as these conditions are correct, I believe your code should work fine.