- Home /
problem with my simple script please help.
here is my script
using UnityEngine;
using System.Collections;
public class countdowntimer : MonoBehaviour
{
public float timer;
public float timerLimit = 30.0f;
public GUIText timerText;
// Use this for initialization
void Start ()
{
timer = timerLimit;
SetTimerText ();
}
// Update is called once per frame
void Update ()
{
timer -= Time.deltaTime;
SetTimerText ();
if(timer <= 0.0f)
{
print ("You Lose!");
timer = timerLimit;
}
}
void SetTimerText()
{
timerText.text = "Time Left: " + timer .ToString("f0");
}
}
the problem is it wont print the message in this case YOU LOSE! i have the same problem with my win script aswell. everything else works it just wont print when the float is equal to or less then 0.0f. what is going on?
Have you checked your console window for any errors or warnings?
Are you absolutely sure that this script is attached to something in the scene?
If I comment out the internals of SetTimerText
, and set timerLimit
to just 3 seconds, I end up seeing the message shortly after I start the scene.
there are no errors or warning in the console. and the script is attatched to the empty game object as a script guitext
and it will countdown from whatever i set it to but when it gets to zero it wont print the message
it also wont print the win text here is the script
using UnityEngine; using System.Collections;
public class FinishLine : $$anonymous$$onoBehaviour { public GUIText WinLoseText;
// Use this for initialization
void Start ()
{
WinLoseText.text = "";
}
// Update is called once per frame
void Update ()
{
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "boulder")
{
//print ("You Win! :)");
WinLoseText.text = "YOU WIN :)";
}
}
}
Get rid of the start, make sure the boulder is tagged as a boulder, and make sure you're using a text and not a 3D text. I just tried it and in the debug window it showed the text "YOU LOSE!" (except I just added it to a game I'm working on so it did it whenever I clicked the left mouse button. I'm not a very good scripter, I just wanted to be of help, try it, let us know.)
Answer by awest · Jul 16, 2014 at 02:29 AM
I can confirm that both scripts have no errors. I have tested them by themselves with only the necessary objects and they functioned as expected. So it must be something in the way the scene is setup. Or some ghost in the machine.
This is a list of my best guess issues. I know some are redundant. Sorry.
Are the scripts in the scene?
Is more than one object changing the gui text?
Are all of the public variables assigned correctly?
Are your tags set with correct spelling/capitolization?
Is the On trigger function on an object with a collider set as trigger?
Does one object that's colliding have a rigidbody?
have you tried restarting Unity or moving everything to a new project?
Others may add to this list if you think of anything.
yes scripts are attatched to the objects in the scene. only one object is assigned to gui text all variables are assigned. tags are set properly. on triger is set to on an empty game object with a collider. the colliding object has a rigidbody. ive tried restarting unity and my computer. but i down know how to move everything into a new project
To move everything to a new project you can start a new project and copy/paste in assets. I've never actually heard of this solving a problem, but I have heard of many people trying it.
I would get these scripts working and then bring in other assets until it breaks(or doesn't) It might help locate the problem.
One thing just occurred to me. Are you working in 2D? Because then you'd have to use OnTriggerEnter2D().
Answer by Slider_j · Jul 16, 2014 at 06:03 AM
where you put -
if(timer <= 0.0f)
{
print ("You Lose!");
timer = timerLimit;
}
Add in "loseFlag" as a public boolian (which is =false) and you could always swap it for -
public bool loseFlag = false;
.
.
.
if(timer <= 0.0f)
{
loseFlag = true;
timer = timerLimit;
}
and add in the routine -
void OnGUI(){
if(loseFlag){
GUI.Label(Rect(0,0,Screen.width,Screen.height),"You Lose");
}
}
hopefully that will work for you! :)