- Home /
Help with script please, boss and destroyed message
I have this script (below) as a sort of lifebar for my boss in game.
All is ok but the one problem I have is the part highlighted in bold.
I use this to instantiate 3d text on screen once the boss is defeated.
The problem I have is that the text keeps reproducing, I think this is because it is in the function Update() part of this code but I don't know how to seperate it from where it is without messing up the script.
If anyone can give me any pointers or help please do! I'm still very new to coding and trying to pick it up by tutorials and trying out things for myself although my knowledge and vocabulary with scripting is rubbish at the moment
var dead : Texture2D; //dead
var lives : Texture2D; //one life left
var lives2 : Texture2D; //2 lives left
var lives3 : Texture2D; //3 lives left
var lives4 : Texture2D; //4 lives left
var Prefabtargetdestroyed_text : GameObject;
var targetdestroyedtext_holder : GameObject;
static var LIVES = 4;
function Update (){
switch(LIVES)
{
case 4:
guiTexture.texture = lives4;
break;
case 3:
guiTexture.texture = lives3;
break;
case 2:
guiTexture.texture = lives2;
break;
case 1:
guiTexture.texture = lives;
break;
case 0:
guiTexture.texture = dead;
**var targetdestroyed_text : GameObject = Instantiate(Prefabtargetdestroyed_text, targetdestroyedtext_holder.transform.position, targetdestroyedtext_holder.transform.rotation);
Prefabtargetdestroyed_text.transform.parent = targetdestroyedtext_holder.transform;**
}
}
Answer by hijinxbassist · Apr 25, 2012 at 10:09 PM
A simple solution would be to add a boolean.
public var showText:boolean=true;
//function Update()
if(showText)
{
//switch(....
case 0:
guiTexture.texture = dead;
**var targetdestroyed_text : GameObject = Instantiate(Prefabtargetdestroyed_text, targetdestroyedtext_holder.transform.position, targetdestroyedtext_holder.transform.rotation);
Prefabtargetdestroyed_text.transform.parent = targetdestroyedtext_holder.transform;**
showText=false;
}
Wrap the entire switch or just the contents of case 0 in the boolean, in start make sure it is true. Once the gui texture has been changed, show text is false so we dont keep running the code over and over.
If you want to make it a bit more efficient, i would add another var lastLifeCount.
public var lastLifeCount:int;
static var lives:int=4;
function Awake()
{
lastLifeCount=lives;
}
function Update()
{
if(lives!=lastLifeCount)
{
SwitchTexture();
}
}
function SwitchTexture()
{
switch(lives)
{
//All lives cases
}
lastLifeCount=lives;
}
This solution is preffered, and you dont need the showText boolean. This will only change textures once you lose a life. You can do away with case 4 and just set that texture from awake or start.
Hi, hijnxbassist, I tried implementing the first solution you suggested. Have I done this correctly because it's still doing the same thing
current code:
var dead : Texture2D; //dead
var lives : Texture2D; //one life left
var lives2 : Texture2D; //2 lives left
var lives3 : Texture2D; //3 lives left
var lives4 : Texture2D; //4 lives left
var Prefabtargetdestroyed_text : GameObject;
var targetdestroyedtext_holder : GameObject;
public var showText:boolean=true;
static var LIVES = 4;
function Start(){
var showText=true;
}
function Update (){
if(showText)
{
switch(LIVES)
{
case 4:
guiTexture.texture = lives4;
break;
case 3:
guiTexture.texture = lives3;
break;
case 2:
guiTexture.texture = lives2;
break;
case 1:
guiTexture.texture = lives;
break;
case 0:
guiTexture.texture = dead;
var targetdestroyed_text : GameObject = Instantiate(Prefabtargetdestroyed_text, targetdestroyedtext_holder.transform.position, targetdestroyedtext_holder.transform.rotation);
Prefabtargetdestroyed_text.transform.parent = targetdestroyedtext_holder.transform;
}
}
@carter All you need to add is showText=false in at the end of case 0
Remember to flip the boolean to true again when you restart the level, otherwise it will display the last active texture.
Add var last live count like the 2nd ex i provided, and check lives against that in update to see if the texture need to be changed would be a better solution. That should work for you tho
Im going to try to be clear as to what is happening currently.
Every Update frame(which happens a lot) you are checking how many lives you have. Then, regardless if its the same amount of lives as last frame, you switch textures.
This is unnecessary texture switching. Since you know when you want the 'switch texture code' to run (everytime the life count changes), you should only execute the code once at that time.
Do this by checking how many lives you had the last update frame. If the amount of lives you had last frame isnt the same as now, then run the switch case for the lives and switch to the appropriate texture. This is in the 2nd ex i provided.