- Home /
How Can I Make it Where Text Shows and the Game Ends on Collision?
The code I have is:
function OnTriggerEnter (other:Collider){
guiText.text = "You Win!";
print("You win!");
Application.Quit();
}
Oh and by the way, the text is attached to the object and the GUILayer is enabled. But the text does not show up, and the game does not end. The debug "You win!" I just put in to make sure it wasn't a collision problem which it is not. If anyone happens to have an idea as to what it is that I'm having issues with, I would greatly appreciate some help in resolving it. Thanks in advance.
In what environment are you running the code? In the Unity editor perhaps? It also strikes me that if you are saying that the text should change and then immediately "Quit" the game ... perhaps the logic to show the changed text is skipped as you immediately quit the game?
I don't know what you mean by environment, I attached this code to the object to be collided with. I figured out a different way to achieve this, but the "Application.Quit()" I'm still not sure is working. Do I have to put guiText.text in a OnGUI function is that why it didn't work this way?
Answer by by0log1c · Apr 09, 2012 at 11:43 PM
The Unity Editor will not close on Application.Quit(). Built project will.
Answer by ztriv · Apr 10, 2012 at 11:16 PM
I meant to put this in my previous post, but the Tab button doesn't work and I didn't know it.
var a = false;
function OnTriggerEnter (other : Collider) {
print ("Testing collision");
a = true;
Application.Quit();
}
function OnGUI(){
if (a == true){
GUI.Label(Rect(Screen.width/2,Screen.height/2,600,60),"You win!");
}
}
function Update () {
}