Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by carter-carl30 · Apr 25, 2012 at 09:20 PM · instantiatescriptingbasicsscriptingproblem3dtextlives

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;**
 
         
 
 }
 
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hijinxbassist · Apr 25, 2012 at 10:24 PM 0
Share

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.

avatar image carter-carl30 · Apr 27, 2012 at 07:42 PM 0
Share

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;

     }

}

avatar image hijinxbassist · Apr 27, 2012 at 08:29 PM 0
Share

@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

avatar image hijinxbassist · Apr 27, 2012 at 08:43 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

instantiate 3d text with different names - c# 1 Answer

Fixing Inverted Camera Controls When Flipped, MouseOrbit 2 Answers

Instantiated enemy doesn't work. 1 Answer

What's wrong with this simple script? 1 Answer

Using variables from UnityScript in Boo? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges