Variable in another is not getting called; stopping next action

Hey everyone,

I’m having a strange problem, that I’m hoping you can help me with. The description may be a little long, but it is kind of tough to explain what is happening.

I have a scene where a player enters a trigger zone, presses the ‘E’ key and then gets teleported to another area, the player is then held in that second area for a certain amount of time and then teleported back.

Within this interaction I have a Boolean (confirmTarget) and an integer that interact with the teleporter. When the player steps into the trigger zone, it sets confrimTarget to true and then the player is able to press ‘E’ to teleport themselves. This part works correctly:

void Update ()
	{
		//if player enters container zone and player hits E, add 10 to confirm target in order to stop flames
		//Teleport player to black screen
		if (Input.GetKeyDown (KeyCode.E) && confirmTarget == true)
		{
			hiddenObject.ConfirmTarget += 10;
			playerTeleport.teleport();
		}

	}

Once the player hits ‘E’ it triggers the teleport function in the playerTeleport script. Within this code I have lines that should reset the Boolean to false and reset the integer. However, with these lines added, the teleporter no longer works. If I remove them, it does work. Here is my code:

public class PlayerTeleport : MonoBehaviour {
	private HiddenObject hiddenObject;
	private GameController gameController;
	public float wait;


	void start () {
		GameObject ContainerObject = GameObject.FindWithTag ("TargetA");
		if (ContainerObject != null)
		{
			hiddenObject = ContainerObject.GetComponent <HiddenObject>();
		}
		GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
		if (gameControllerObject != null)
		{
			gameController = gameControllerObject.GetComponent <GameController>();
		}
			}

		public void teleport () {
		gameController.confirmTarget = false;
		hiddenObject.ConfirmTarget = 1;
		transform.position = new Vector3(0,1,60); 
		StartCoroutine (TeleportBack ());

				}
	//Teleport player in random rotation back to dis Room
	IEnumerator TeleportBack() {
		yield return new WaitForSeconds (wait);

		transform.position = new Vector3(0,1,0);
		transform.rotation = Quaternion.Euler (0.0f, -90.0f, 0.0f);
		}


	}

i also recieve this error in the console :

NullReferenceException: Object reference not set to an instance of an object
PlayerTeleport.teleport () (at Assets/Scripts/PlayerTeleport.cs:25)
GameController.Update () (at Assets/Scripts/GameController.cs:38)

Any thoughts on why changing a variable in another script might be halting the whole process?

Thanks!

Your start() method will not be called ever since it is not correct punctuation.

void Start() // the Start() Method is case sensitive.
{

}

So since the method does not get called your gameController object never gets initialized properly and thus you get an NullReferenceException when you try and access the Boolean variable in your teleport() method.

Huh, thanks. I was able to fix it but I don’t really understand why my solution worked, since I used the same method in my previous scripts.

All I did was move the gameController and hiddenObject objects from void start, to void teleport like so:

void start () {
				
				}

		public void teleport () {

		GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
		if (gameControllerObject != null)
		{
			gameController = gameControllerObject.GetComponent <GameController>();
		}
	
		GameObject ContainerObject = GameObject.FindWithTag ("TargetA");
		if (ContainerObject != null) {
			hiddenObject = ContainerObject.GetComponent <HiddenObject> ();
		}

		gameController.confirmTarget = false;
		hiddenObject.ConfirmTarget = 1;
				transform.position = new Vector3(0,1,60); 
		StartCoroutine (TeleportBack ());
						}

In other scripts I have the code to reference another script in void start. Why didn’t it work this time?