Hi there, In a racing gaming I want a timer to begin a countdown when the car enters in a specific road. When the car exits this road and drives on another road I want the timer to reset and start the countdown again. so far... I created a plane (for a road) and I attached to it a guiText object as a timer (like in the marble tutorial). I created a prefab and dragged in the prefab the plane (with the attached guiText). Then I took the timer script (from the tutorial) and I changed the function Update() to function OnCollisionEnter () and I attached it onto the plane prefab. When I run the game and the car is moving onto this plane I got a message... "there is no 'GuiText' attached to the Plane01 object but a script is trying to access it"... but the guiText is attached to the plane...what am I doing wrong??
It would help to see the code you're using, as well as a picture of the Inspector when the plane is selected.
The script is from the MarbleTutorial. I have changed the function Update to function OncollisionEnter(). Code (csharp): var startTime = 10.0; function OnCollisionEnter () { // The time left for player to complete level! timeLeft = startTime - Time.time; // Don't let the time left go below zero. timeLeft = Mathf.Max (0, timeLeft); // Format the time nicely guiText.text = FormatTime (timeLeft); } // Format time like this // 12[minutes]:34[seconds].5[fraction] function FormatTime (time) { var intTime : int = time; var minutes : int = intTime / 60; var seconds : int = intTime % 60; var fraction : int = time * 10; fraction = fraction % 10; // Build string with format // 12[minutes]:34[seconds].5[fraction] timeText = minutes.ToString () + ":"; timeText = timeText + seconds.ToString (); timeText += "." + fraction.ToString (); return timeText; } The inspectorView of the plane is in the attached pic Another idea is telling with a script 'if the car is grounded start the timer, when the car is not grounded reset the timer'. I'm trying to work it out at the moment.
Well, the first thing to note is that there is no GUIText component attached to your plane. Note that "attached" means attached directly to the object, either by dragging a script to the object or by choosing something from the Component menu while the object is selected. What you may have is a GameObject which is a child of the plane and has its own GUIText attached. This will not suffice for the script in its current state, because guiText is a reference to a component attached to the same object as the script. The script probably won't do what you want in its current state, however. You need an Update() function in order to display the time. Otherwise it will only change when you enter a collision. What are you resetting the timer for? Are you going for a checkpoint system where you have a certain amount of time to get through each part of the track?
well, I'm trying to make something like a 'Driving On The Mix' game. I have a road wich is consisted of 3 smaller roads. Each one of the smaller roads volumes up one of the 3 tracks of the soundtrack everytime the car is driving on it. At the same time you can collect spheres that trigger other random sounds, which contribute to the soundtrack. I just need a timer so that I can "force" the player not to drive all the time on the same small road. For example, the car enters in a small road and a timer starts a 20seconds countdown. The player then must collect as many spheres as he can and then change route to the small road at the side before the timer reaches to zero. when he enters the new road then the timer resets and starts the countdown again and so on.
an even simpler idea maybe will be to change the text of the guiText object everytime there is a collisionenter. something like that??? Code (csharp): function OnCollisionEnter () { GameObject.Find("Timer").GetComponent(guiText).text = "0:10.0"; } it doesn't work though. Probably there is an error in the script which try to change the text of the "Timer" guiText object.
there was a spelling mistake: Code (csharp): function OnCollisionEnter () { GameObject.Find("Timer").GetComponent(GUIText).text = "0:10.0"; } I attached this scrip to the plane. It's correct but it only works when the timer is not running. So, I think I have to stop the timer before I change the GUItext back to 10 seconds. Or I could just set the timeLeft var of the time script back to 10 seconds. Any Ideas???
For situations where you have a central timer or controller, I prefer to break it into it's own script as a controller. So what you do is create a timer script, with public functions for starting and stopping the timer, and whatever else you need. This "Timer Controller" is then placed on an empty GameObject in your scene. Now all you do is get a reference to the timer controller script (either set up a var and drag it to whatever script in the inspector, or better, find it on startup.) Here's some basic code to give you an idea Code (csharp): //++++this is the timer controller. It is placed on it's own GO++++ public var myGUIText; //our GUIText public var timer = 0.0; public var running = false; function Update() { if (running) { //update the timer and the GUI } } public function StartTimer() { //start the timer running = true; } public function StopTimer() { //stop the timer running = false; } public function ResetTimer() { //etc timer = 0.0; } //+++++++++++++++++++++++++++++++++++++ //now our other code file which accesses the timer var timer; function Start() { timer = GameObject.Find("Timer Controller").GetComponent("Timer Script"); function OnCollisionEnter() { //start the timer timer.StartTimer(); } function OnCollisionExit() { //stop the timer timer.StopTimer(); } Don't try to use that code as is because a: I dont code JS an I haven't defined the types of vars and such, and b: it is just meant to give you an idea of setup and design. ;-) HTH, -Jeremy
Thanx a lot lot lot!!!!! I'm reposting the scripts for anyone who is interested in to see them complete. I created the script for the Timer and I attached it on the car. Timer.js : Code (csharp): var seconds = 11.0; var timertext : GUIText; var running = false; function Update () { if (running) { timertext.text = "change track in " + Mathf.Floor(seconds); seconds -= Time.deltaTime; } if (seconds < 1) { Application.LoadLevel ("Level1"); } } function StartTimer() { running = true; } function StopTimer () { running = false; } function ResetTimer() { seconds = 11.0; } and then a script to restart the timer everytime the car change road. controlTimer.js: Code (csharp): var timer; function Start(){ timer = gameObject.Find("Car").GetComponent(Timer); } function OnCollisionEnter(){ timer.StartTimer(); } function OnCollisionExit(){ timer.ResetTimer(); } It seems so simple now that I see it finished!! But I don't think I would have ever thought braking it into 2 sub-scripts. Thanx again!
Haha, np. I am glad you got it working. Sometimes you get to close to your work and the "forest from the trees" thing kicks in. Helps to get a different perspective. The Unity IRC is awesome for this. ;-) -Jeremy