- Home /
4.6 Trigger.js Wrong command is running. Help?
I'm having problems with a script I created called Trigger.js. The script is suppose to show a GUITexture, buttons, and tell whether you won or lost. If the Opponent passes the finish line first, it is suppose to show "You Lost". If the player wins, it is suppose to show "FINISHED" GUI Texture. Problem is when the Player passes the finish line, it runs the wrong command so it shows "You lost" instead of "FINISHED". The opponent has the tag "Opponent" and the Player has the tag "Player". Here is the script:
var finished : boolean = false;
var FinishedTexture : Texture2D;
var Car = GameObject;
var lost : boolean = false;
var MainMenuLevel : String;
var RestartLevel : String;
var NextLevel : String;
var LostText : String; var style
: GUIStyle;
function Start(){
Screen.showCursor = false; }
function OnTriggerEnter(Col : Collider) {
Debug.Log("Finished!");
if(GameObject.Find("Opponent").CompareTag("Opponent")){
lost = true;
yield WaitForSeconds(5);
Application.LoadLevel(RestartLevel);
}else{
finished = true; }
if(finished) {
OnGUI();
Screen.showCursor = true;
} }
function OnGUI() {
if(GameObject.FindGameObjectWithTag("Player")){
if(finished) //Means If finished is true
{
GUI.Label (Rect (475, 40, FinishedTexture.width,
FinishedTexture.height/2 - 150),FinishedTexture);
if(GUI.Button (Rect (Screen.width/2 -
150,Screen.height/2, 300, 40),"Restart")){
Debug.Log ("Restart button works");//Test to see if button work
//Reloads the Scene Level \/
Application.LoadLevel(RestartLevel);
}
if(GUI.Button (Rect (Screen.width/2 -
150,Screen.height/2 - 100, 300, 40),"MainMenu")){
Debug.Log("MainMenu Button works!");
Application.LoadLevel(MainMenuLevel);
}
if(GUI.Button (Rect (Screen.width/2 -
150,Screen.height/2 - 50, 300, 40),"NextLevel")){
Debug.Log ("Next Level Button Works!");//Test to see if button
works
//Reloads the Scene Level \/
Application.LoadLevel(NextLevel);
}
} } if(lost) {
GUI.Label (Rect (300, 40, Screen.width/2 - 150, Screen.height/2 -150), LostText, style); }
}
What the opponent is:
Here is what happens when I pass the finish line first.
Script vars:
If anyone knows how to fix this, please help.
Your answer
Follow this Question
Related Questions
JavaScript OnGUI GuiSkins aren't working, multiple scripts 1 Answer
Basketball Score counter 1 Answer
Unity v4.6 Joystick? 2 Answers
Get status of GUI Button 4.6 2 Answers