@darlaten
I hadn’t looked at the tornado-twins tutorial much before earlier today… I decided to go through it real quick after seeing your questions in the UnityAnswers Q/A and see for myself what the “whole” tutorial was because it seemed like you might not have went through the entire thing. … But, the tutorial abruptly ends after video 28 or so, and it seems they never finish “everything” they were planning to show in it at first.
Unless they have more video’s for it that I didn’t see on their youtube channel… … Does anyone else know if they do??
Anyway, so, after reading your questions in the Q/A I’m guessing you’re trying to take over from where the twins dropped-out, right?
I noticed earlier after getting to the end of their last video that the switch-statement you’re trying to modify in your code was left unfinished in the tutorial with nothing but
case : 0
//game over here
break ;
and I wondered if I had accidentally skipped a video with that part in it …but, from the looks of things, I guess that’s just how they left it.
So, what are you trying to do for the game over exactly? Just instantiate an explosion and then re-load the main-menu?
… Lets start from the top of that script.
var health1 : Texture2D ; //one health
var health2 : Texture2D ; //two health
var health3 : Texture2D ; //full health ;
var bodypart1 : Transform ;
var bodypart2 : Transform ;
Add this variable for your worm’s head body part, just to try and make sure we get this working right… I’ll explain what it’s for later.
var worm : Transform ; //drag the worm head on here
Then your explosion variable.
var explosion : Transform ;
And now add this true/false (boolean) variable as well… I’ll explain later what we’re going to use this variable for.
private var exploded : boolean = false ; //did we run out of lives and blow up??
–continue with the code you already have for now->
static var LIVES = 3 ;
static var HITS = 0 ;
function Update () {
print("Lives : " + LIVES + " Hits : " + HITS) ;
switch(LIVES){
case 3 :
guiTexture.texture = health3 ;
break ;
case 2 :
guiTexture.texture = health2 ;
break ;
case 1 :
guiTexture.texture = health1 ;
break ;
case 0 :
//gameOver here
Alright, now we have zero lives left.
You’re wanting to instantiate an explosion when you’re worm dies it’s last death? Then wait for so many seconds before going back to the main menu…
Ok, but the Unity function “Update()” can not be used as a coroutine and it cannot “yield” for any amount of time. Update() runs continuously. So, we have to make another function to call, which will run in the background while Update() continues on the front-line. You can make any function you want, and name it whatever you want (other than the Unity function names such as Update() ; FixedUpdate() ; OnGUI() ; etc… those are non-negotiable and are-what-they-are) … So, for example, if you want to make your own function and name it
“WormRanOutOfLivesSoKillitDeadForGood”
you could name it exactly that.
We’ll go with something less-likely to wear out our keyboard’s though. How about we name it “EndGame” , make a call to it in the script, and then jump down to coding that function.
EndGame() ;
break ;
}
switch(HITS){
case 1 :
bodypart2.renderer.enabled = false ;
break ;
case 2 :
bodypart1.renderer.enabled = false ;
bodypart2.renderer.enabled = false ;
break ;
case 3 :
LIVES -= 1 ;
HITS = 0 ;
MoveAround.dead = true ;
bodypart1.renderer.enabled = true ;
bodypart2.renderer.enabled = true ;
break ;
}
}
function EndGame(){
So, now we’re inside our own new function named “EndGame”, and it’s time to use the boolean (true/false) variable “exploded” that we added at the top of the script earlier.
Since this function EndGame() is being ran inside the Update() function and our lives being at zero is what makes Update make a call to it, then it will be getting called every frame of Update() (TornadoTwins said a billion times per second as a joke in their tutorial).
We’re going to destroy our worm, and instantiate an explosion in this function. We only want it to do this one time, not one-time-every-frame-per-second… so, we have our boolean “exploded” variable to let it run once, and no more than that.
if(!exploded){ //if not blown up yet, blow up
The TornadoTwins were a bit goofy with their script placements and a few other things on this tutorial, so our worms health script placement (if you did it exactly how they said to) is on some GUI-TEXT object instead of our worm, and that’s script we’re modifying here; so since you’re wanting to blow your worm up, you need to instantiate the explosion where the worm is, not where the GUI is (which is what your code would have done, it would have made an explosion at that GUI object’s position) .
Since we’re going to drag/drop our worm’s head on to the variable “worm” that we added at the top earlier, we’ll use that for the explosion position and rotation. Then we’ll Destroy() all of the worm body parts variables and make sure it’s dead.
var exp = Instantiate(explosion, worm.position, worm.rotation) ;
Destroy(worm.gameObject);
Destroy(bodypart1.gameObject);
Destroy(bodypart2.gameObject) ;
Now that we instantiated an explosion, and destroyed our worm completely, I’d say we have in fact “exploded” , so we need to set that variable to “true” in order to keep the Update() function from trying to continue making us explode.
exploded = true ; //we're blown up now, so stop blowing ourselves up
}
Almost done… our worm is dead, and blown up, and now we want to give the player a few seconds to let the bad news sink in a little deeper before making them start the game all over from the start again. Since we’re coding in function EndGame() still, and not in Update(), that means we can yield for however long we want before continuing the rest of this function. EndGame will take a seat in the background counting time while Update() keeps running up front, and then when our wait-time has expired EndGame will start back up again where it left off.
yield WaitForSeconds(5) ;
Application.LoadLevel(0) ;
}
Hope that helps you out with this script anyway, I remember seeing a couple of your other posts in the UnityAnswers Q/A. I’ll go take a look at those also, and see if I can’t help you get the rest of your code working properly since I know what the whole worm-thing is about now.
That TornadoTwins tutorial seem’s to be something to get people going with unity basics, and looks like a good starting point. It would be nice if they’d correct their errors and finish what they started though, I’ve seen a few other people’s post’s in Q/A for this tutorial and having problems with it also.
If you post more questions in the Q/A, just try to be specific and descriptive of what/where your problem is occurring. People got pretty heavy with the downvotes on you in there yesterday because your questions were too vague and/or sloppy. Don’t worry about your spelling, we can read what your saying. But when you post code, try to get it formatted in a code-block properly because it’s a pain in the ass to read when it’s not.
–Lo0NuhtiK