C# script not working (143522)

I have a script that is suppose to display test on the screen and play an animation when the player walks through a box collider only it doesn’t work. When the player walks through the collider nothing is played. I have several other scripts that are almost identical to this and they all work perfectly so I don’t understand the problem.

This is the script…

using UnityEngine;
using System.Collections;

public class LightScene3 : MonoBehaviour {

	public int wait;
	public GUIStyle text;
	string message = " ";
	
	
	//GUI Box
	void OnGUI() {
		GUI.Label((new Rect(600,400,200,200)),message ,text);
	}
	
	//Displaying text
	IEnumerator OnTriggerEnter(Collider otherObjective){
		if (otherObjective.tag == "Player") {
			
			message = "Insert Text Here";
			GetComponent<UnityEngine.Animation> ().Play ("TeacherMonster");
			yield return new WaitForSeconds(wait);
			message = "Insert More Text Here";
			yield return new WaitForSeconds(wait);
		}
	}
}

What part of your script were you expecting to display "test" on the screen? Same answer as for all collision-doesn't-work questions: - You need colliders on both objects, one marked as Is Trigger - You need a Rigidbody on at least one object - Get rid of the tag test, and just use Debug.Log to confirm that OnTriggerEnter is being called. You can print the name of the triggering Gameobject - it may not be what you think it is.

–

2 Answers

2

AFAIK, the method declaration for OnTriggerEnter is a bit different from the one you have written like this:

IEnumerator OnTriggerEnter(Collider otherObjective){

You should try void instead of IEnumerator:

void OnTriggerEnter(Collider other) {
     // Your onTrigger logic here
}

Reference doc

Hope it helps!

I have to use the IEnumberator in order to use WaitForSeconds. I tried it out anyway and it still doesn't work. Thank you for your help though.

–

OK. I solved the problem and realised that the collider was falling through the floor as it was attached to an object that was falling through the floor. Silly me!

Please could a moderator delete this question. Thank you!