|
Hi Everyone, Over the last couple of months I've been trying to wrap my head around some programming in Unity but I am NOT a programmer, so I've been running into a lot of roadblocks. I've been working with a script to animate textures that I grabbed from the Unity Wiki:
I'm using this texture on gameObject that is instantiated by another. The texture shows a sequence of numbers counting down. The problem I'm having is that the countdown seems to be based on a global time, rather than the amount of time the object has been around for. I could have a hundred of these objects on screen, all generated at different times, but they will all show the same frame of the texture. Is there a way to have the time based off when the object was instantiated, rather than when the program was first run? I've gone through the forum as well as the script reference and I can't find anything that describes what I'm looking for. Any help would be greatly appreciated! Thanks.
(comments are locked)
|
|
Hey man, You could handle this in multiple ways. Using what you have you could use a boolean that tracks the game state and an offset for the time it actually starts working - for example (remember to add your other variables and code from your first script): A tidier way to do it would be to have a co-routine running every second which counted just shifted the texture offset incrementally. In this pesudo example you kick of the co-routine by sending the startCountDown message to your countdown object. EDIT If you are instancing these objects and want them to count from when they are created do this: Hi, Thanks for the response! I'm trying out your first suggestion and aside from one error it seems to be working just fine. The first time one of the countdown objects is instantiated the game pauses with an error: "NullReferenceException: Object reference not set to an instance of an object" If I select the instanced game object (ballCountdown(Clone) it allows me to add an object through the inspected under the Game Controller variable. The only object I can select though is ballCountdown(Clone). If I unpause the game it will run perfectly until the next countdown ball appears, then I have to do this all over again. The most important part though, is that the textures have the proper offset, so it's almost there. I'm pretty sure this is the part of the script that's giving me trouble as it's the only part I don't fully understand: var gameController : destroyAfterTime; var startTime : float; /* other variables listed here */ function Update(){ if(gameController.countDown){ destroyAfterTime is a script I wrote that will blow up the ball after the countdown has finished. It uses the variable "countDown" to determine how long before the ball detonates. I think I'm just lost on the term "gameController".
Jun 10 '11 at 11:50 AM
OtsegoDoom
Hey man, I've edited the answer so you can use this on instanced objects. gameController was a variable for a master control script but I suspect you don't need that.
Jun 10 '11 at 12:01 PM
demize2010
It worked! Thank you so much. I actually understand what's going on in your script which I think it the most important part. Here's the complete script I used in case anyone wants to use it: var startTime : float; var uvAnimationTileX = 4; //Here you can place the number of columns of your sheet. var uvAnimationTileY = 1; //Here you can place the number of rows of your sheet. var framesPerSecond = 1.0; function Awake(){ startTime = Time.time; } function Update(){ } This script will give your object an animated material that will start from the first frame when the object is instantiated. Thanks again!
Jun 11 '11 at 12:18 AM
OtsegoDoom
(comments are locked)
|
