Can you help me understand where im going wrong with my code?

Basicly im getting into using Csharp for my university project ive wayched a few Csharp Overview tutorials which describes Variables, Functions and classes so i tried to create my own code.

What i want my code to eventualy do is change scene on click of the key E when i reach a trigger collider. But for now i just want to be able to change scene by clicking E so i compiled this code and variations of it that i binned hoping to help my understanding of how it works out with no avail.

Code as follows:

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

public class Scenechange : MonoBehaviour {

	void OnTriggerEnter (Collider other)

	{
		if (Input.GetKeyDown ("e"))
			SceneManager.LoadScene ("new scene");
	}

	}

if some one could be kind enough to help me understand my errors i would be forever in your
debt

Kind Regards

Christopher Touhey

Hi there @cjtouhey

You have only one problem that I can spot and I’ll run you through it.

The first thing I’ll state isn’t necessarily an error and you may have just made a typo, but it is an important convention which you would do well to follow just in case you didn’t already know. The name of your class currently is:

public class Scenechange : MonoBehaviour

A common convention in programming is to name all public class names and public methods using PascalCasing. This is a convention whereby a programmer will begin every new word with a capital letter. For instance, “MyNewWord” would be an example of PascalCasing as every new word began with a capital letter. So your class name would ideally be named:

public class SceneChange : MonoBehaviour

Now on to the problem.

The error/problem you’re likely receiving is due to the fact that you are checking for input within a method that only fires once when the player enters. This means, that you would have the press the “e” key at exactly the same time as when you enter the trigger as it never checks the conditional again until you re-enter the trigger. This isn’t an error as such, as the game won’t break and the compiler won’t throw an exception, but it does mean that you’ll never be able to change scene by pressing the “e” key.

If you want to constantly poll for user input when in a trigger then you could try either of these two ways.

Option One

We use the OnTriggerStay() method to continually check if the user is within the trigger and if they press “e”.

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

     // Recall's the method for almost every frame the player stay's within the trigger.
     void OnTriggerStay (Collider other)
     {
         if (Input.GetKeyDown ("e"))
             SceneManager.LoadScene ("new scene");
     }
}

Option Two

We use a bool flag to decide when the player is or isn’t within the trigger. We then check this flag within the update method to decide when the player can or cannot press “e”.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement; 
 
 public class Scenechange : MonoBehaviour 
{
     // A bool that will decide when the player has left or entered a trigger.
     private bool m_IsInTrigger = false;

     void OnTriggerEnter (Collider other)
     {
          // Player has entered trigger, set bool flag to true.
          m_IsInTrigger = true;
     }

     void OnTriggerExit (Collider other)
     {
          // Player has left trigger, set bool flag to false.
          m_IsInTrigger = false;
     }

     void Update ()
     {
          // Is the player in the trigger?
          if(m_IsInTrigger)
          {
               // Check for input.
               if (Input.GetKeyDown ("e"))
                           SceneManager.LoadScene ("new scene");
          }
     }
}

If you want to learn more about the OnTriggerStay and OnTriggerExit methods please consult the API links below:

OnTriggerStay() - API

OnTriggerExit() - API

I hope this helps with what you were looking for! :slight_smile: