- Home /
Do not delete GUI after 2nd collision
I've found a script for activating GUI on collision (link). Here's the script :
using UnityEngine;
using System.Collections;
public class messagebox : MonoBehaviour {
public Transform other;
void OnGUI() {
if (other) {
float dist = Vector3.Distance(other.position, transform.position);
if(dist < 10 ){GUI.Box (new Rect(0,0,100,100),"Well done!!");
}
}
}
}
But after second collision (bouncing ball) the GUI disappears. How I can make the GUI 'lock'? I should use another type of GUI (I mean, not GUI.Box or something different)? I'm trying to change bits of the text, but nothing works. Or maybe I should use GUI.Enabled?
Answer by Dave-Carlile · Dec 27, 2012 at 06:56 PM
You need to use a separate variable to keep track of the fact that you've turned on the box.
public class messagebox : MonoBehavior {
bool displayBox;
void Update()
{
// get the distance
float d = Vector3.Distance(other.position, transform.position);
// once the distance is below 10, turn on the box - it will stay on
// until displayBox is set back to false
if (d < 10)
displayBox = true;
}
void OnGUI()
{
if (displayBox)
{
GUI.Box(new Rect(0, 0, 100, 100), "Well done!!");
}
}
}
"Assets/scripts/win.cs(1,20): error CS0246: The type or namespace name `$$anonymous$$onoBehavior' could not be found. Are you missing a using directive or an assembly reference?"
I'm doing something wrong, because I suck in C#. Changing it to $$anonymous$$onoBehaviour gives me the same error.
Sorry, just typed this in here and using the U.S. spelling of behavior. Just look at what the script is doing rather than copying it directly - add the necessary parts to the script you already have. That will help you understand better what it's doing anyway.
I didn't include the using statements at the top. You'll need those. I'll repeat though, don't just copy & paste it - take the time to understand what it's doing.
Your answer
Follow this Question
Related Questions
How can I check if an object is seen by my camera and if i't inside a gui? 1 Answer
Problems with instantiation... 1 Answer
ScrollView not....scrolling. 2 Answers
C sharp menu problem with bottons 1 Answer
Problem with a circle GUI. 1 Answer