- Home /
Error with trigger PLZ HELP!!!!
I Tryed
function OnTriggerEnter(){
if(GUI.Button(Rect(10,50,50,30),"Enter"))
if (stringToEdit == "times")
{
Application.LoadLevel("Scene2");
Screen.showCursor = true;
}
}
So when the player triggers the cube trigger then this happends but it dont work why not PLZ HELP!!! (im a noob)
Comment
Answer by aldonaletto · Nov 03, 2013 at 03:05 AM
That's a very frequent error: GUI items can only be used inside the function OnGUI. The solution is to set a boolean when the player is inside the trigger and use it to enable the GUI item inside OnGUI:
private var inTrigger = false;
function OnTriggerEnter(other: Collider){
if (other.CompareTag("Player")){ // remember to tag the player as "Player"!
inTrigger = true; // player entered trigger
}
}
function OnTriggerExit(other: Collider){
if (other.CompareTag("Player")){
inTrigger = false; // player left trigger
}
}
function OnGUI(){
// button Enter only appears when the player is inside the trigger
if (inTrigger && GUI.Button(Rect(10,50,50,30),"Enter")){
if (stringToEdit == "times"){
Application.LoadLevel("Scene2");
Screen.showCursor = true;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613121138im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Error: GetComponentFastPath is not allowed? 2 Answers
How to make colliders without using any of the inbuilt physics 1 Answer
Selecting Object From Top Causes NullReference 1 Answer
SCRIPT NOT WORKING 2 Answers