I have a piece of script here that when i hit a sphere it is ment to show GUI about your time and a button that takes you to the next level, it is not working and i have no idea why.
Can anyone help?
var player : GameObject;
private var time = 0.0;
private var ended = false;
function onGUI(){
if(ended){
GUI.Label(Rect(Screen.width*0.5-30,Screen.height*0.5-22,60,20),"Final Time:"+time);
if(GUI.Button(Rect(Screen.width*0.5-50,Screen.height*0.5+2,100,40),"Next Level")){
Application.LoadLevel("Lvl2");
}
}
}
function OnTriggerEnter (hit : Collider) {
if(player.tag == "Player") {
ended = true;
time = Counter.Counter;
}
}
Is your OnTriggerEnter function getting called? Make sure that your player has a rigidbody- OnTriggerEnter will not function otherwise. Add some Debug.Log statements throughout there to find out what parts of the code are actually running.
OK, actually, wait a minute, I've just noticed a heap more places where I bet errors are happening ... most important of which being that you've misspelt OnGUI() ... :D Don't worry, I will rewrite this script for you ... :)
OK, I deleted my previous answer and am beginning another one. If these revisions don’t work, then I don’t know what will. Revisions are documented in comments:
private var time : float = 0.0;
private var ended : boolean = false;
function OnGUI(){ //OnGUI was misspelt here, meaning function was not actually being called
if(ended) {
GUI.Label(Rect((Screen.width * 0.5) - 30, (Screen.height * 0.5) - 22, 60, 20), "Final Time: " + time.ToString()); //Added some more brackets just in case compiler was messing up order of operations and misplacing position
if(GUI.Button(Rect((Screen.width * 0.5) - 50, (Screen.height * 0.5) + 2, 100, 40), "Next Level")) { //Did the same here
Application.LoadLevel("Lvl2");
}
}
}
function OnTriggerEnter (hit : Collider) {
if(hit.collider.gameObject.tag == "Player") {
ended = true;
time = Counter.Counter; //What is Counter.Counter? Is there more to this script? Left it in anyway ...
}
}
Is your OnTriggerEnter function getting called? Make sure that your player has a rigidbody- OnTriggerEnter will not function otherwise. Add some Debug.Log statements throughout there to find out what parts of the code are actually running.
– syclamothDid you try my suggestion in the answer below? :P
– KleptomaniacSo my suggestion regarding "if (hit.collider.tag == "Player") {" didn't work either? Hmm ...
– KleptomaniacOK, actually, wait a minute, I've just noticed a heap more places where I bet errors are happening ... most important of which being that you've misspelt OnGUI() ... :D Don't worry, I will rewrite this script for you ... :)
– KleptomaniacAlright, I revised my answer below ...
– Kleptomaniac