Did you mark one of them as the “Trigger”? Those trigger events won’t get called for non-trigger enabled objects. (only enable the trigger on one, not both)
Hmm, what do you mean by one of them? I’m only using one Box Collider for this (although I have several Box Colliders in the scene, but they are for loading levels).
I’ve ticked the “Is Trigger” box and input the code, but I’m getting what seems like a syntax error - the debugger says I’m missing a semicolon. But when I change the highlighted error, more appear.
I’m thinking I may have written the script wrongly in the first place. It’s rather simple and I am a script newbie, so yeah…
Non-trigger collision events seem to work only with other non-trigger collision events. As soon as a trigger is flagged, the “trigger” events get called instead. I’m still not 100% sure why the difference is necessary … anyone?
I tried what you suggested, but it doesn’t seem to be working (reversing the order of the code).
I’m not sure what you mean by the object entering the collider needing a collider/rigidbody attached.
This is because I am using a FirstPersonController and I have not encountered any difficulty when entering a Box Collider which has code to load a next level. Hence I feel that there is something wrong with the code itself. Still, your help is appreciated!
You can’t have the OnTriggerEnter function inside OnGUI. You can get the result you want by having a boolean variable that is set in OnTriggerEnter and tested in OnGUI:-
var triggered: boolean;
function OnTriggerEnter() {
triggered = true;
}
function OnGUI() {
if (triggered) {
GUI.Label(...);
}
}
I was trying to follow this thread to solve a similar problem in my game.
But it seems I am doing something wrong eventhough my code doesnt return any errors.
Im still very new to scripting in UNITY. I have so far only used C# and Javascript for web dev.
This is my code which I think is correct. (But obviously it aint since it wont work for me)
using UnityEngine;
using System.Collections;
using System;
using System.Text;
using System.IO;
public class DialoqueStartScript : MonoBehaviour {
bool triggered;
void OnTriggerEnter( Collider other)
{
if (other.gameObject.tag == "Player")
{
triggered = true;
}
}
void OnGui()
{
if (triggered) //The dialogue starts.
{
GUI.Label(new Rect(300,300,500,200), "Hello World!");
}
}
void OnTriggerExit( Collider other)
{
if (other.gameObject.tag == "Player")
{
//The dialogue ends.
}
}
}
I know the thread here mainly talks about javascript, but could anyone see what I am doing wrong? The GUI.Label aint never displayed when my player object enters the area I have made IsTrigger and my script attached to that.
The firstpersoncontroller has a rigidbody internally present.
to trigger a Trigger, one of the two objects involved in that “collision” must have a Rigidbody component attached beside its collider. if none of the two has one it would have the consequence that triggers won’t fire at all anymore.
as for the code itself:
move the functions out, you can’t not have function within function.
Both are standard callbacks and must be on the class public level. if you need to change a thing on the gui basing on the trigger, you must use a variable.
So you need to have
var labelText : String = "";
function OnGUI() {
GUILayout.Label(labelText);
}
function OnTriggerEnter( other : Collider ){
labelText = "...";
}
I had a much simplier solution but getting an error NullReferenceException: Object reference not set to an instance of an object Showtext.OnTriggerEnter