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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Change scene/level on trigger

Discussion in 'Getting Started' started by amandathorp, Feb 18, 2021.

  1. amandathorp

    amandathorp

    Joined:
    Nov 25, 2020
    Posts:
    8
    Hey everyone, I watched a video about the script to use when you want to change a scene on trigger, but for some reason my Console had soo many errors. Do you have to have a GameFlowManager script as well as a SceneChange script? What could be wrong here...?

    This was the video:
    https://www.youtube-nocookie.com/em...&loop=1&modestbranding=1&playlist=9lPCv9kkbSI

    If anyone else has script for scene changes, that would be great!
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    You don't need all that. By "trigger" do you mean a collider set to trigger? If so, just add a script to your trigger object something like this:

    Code (CSharp):
    1. void OnTriggerEnter(Collider other) {
    2.     Debug.Log("Trigger entered by " + other.gameObject.name);
    3.     SceneManager.LoadScene("SomeOtherScene");
    4. }
    If you get errors, and can't figure out why yourself, then post them here. (Remember that "I got errors" is no good to anyone — you need to post the exact error message.)
     
    Ox_yO and amandathorp like this.
  3. amandathorp

    amandathorp

    Joined:
    Nov 25, 2020
    Posts:
    8
    Thanks, Joe. I added what you said to write this:

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;

    5. public class SceneSwitch : MonoBehaviour
    6. {

    7. void OnTriggerEnter(Collider other)
    8. {
    9. Debug.Log("Ruby" + other.gameObject.Collider);
    10. SceneManager.LoadScene("Scene1");
    11. }
    12. }




    Error message is:

    Assets/Scripts/SceneSwitch.cs(11,41): error CS1061: 'GameObject' does not contain a definition for 'Collider' and no accessible extension method 'Collider' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)


    (I realize I need to declare my Collider gameobject somehow, but I'm very new to coding)
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    OK, let's work through how to figure this stuff out, since it will happen a lot. The error message says it is at SceneSwitch.cs(11,41). That means it's on line 11:

    Debug.Log("Ruby" + other.gameObject.Collider);

    And the complaint is that Collider does not exist. Now compare that to what I wrote:

    Debug.Log("Trigger entered by " + other.gameObject.name);

    There are two differences. One is the string inside the quotes, but as I'm sure you know the compiler doesn't much care about that. The other is, I wrote other.gameObject.name, while you wrote other.gameObject.Collider. And the compiler doesn't like yours. :)

    That's because other.gameObject is of type GameObject, which does not have a Collider property. It does have a name property though. (It's listed under "inherited properties".)

    I'm not sure why you changed it, but change it back and it'll work.
     
    crushmancreator and amandathorp like this.
  5. amandathorp

    amandathorp

    Joined:
    Nov 25, 2020
    Posts:
    8
    Oooh, I thought I was supposed to fill in my own names of the game objects... I know that sounds very stupid. Thank you for taking the time to help!


    I think I'm getting closer, but now it has an error message for line 11 that SceneManager does not contain a definition for 'LoadScene'

    Assets/Scripts/SceneManager.cs(11,18): error CS0117: 'SceneManager' does not contain a definition for 'LoadScene'


    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;

    4. public class SceneManager : MonoBehaviour

    5. {
    6. void OnTriggerEnter(Collider other) {

    7. Debug.Log("Trigger entered by Player" + other.gameObject.name);
    8. SceneManager.LoadScene("Scene1");
    9. }
    10. }
    I'm calling my player 'Player' and my trigger 'Trigger'
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Hmm, this is something different, since SceneManager definitely does have a LoadScene method, as you can see in the docs here.

    However as you can see in that example, you also need
    using UnityEngine.SceneManagement;
    at the top of your script.

    That will probably fix it. The other possibility I guess is that you're using some other (older?) version of Unity, where this particular function has changed. What version of Unity are you using?
     
  7. amandathorp

    amandathorp

    Joined:
    Nov 25, 2020
    Posts:
    8
    I was using the 2020.1.17f1 install, but going ahead and installing the 2020.2.4f1 right now. Thanks for your help and if I run into other errors, I will reference the link you sent. :)
     
  8. amandathorp

    amandathorp

    Joined:
    Nov 25, 2020
    Posts:
    8


    Finally got it by making a button and using this script:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class SceneSwitch : MonoBehaviour {

    public void SceneSwitcher () {
    SceneManager.LoadScene (1);
    }
    }


    Thanks again for taking the time to read and help me! I'm excited to play around with this new skill.
     
    JoeStrout likes this.