Author Topic: NGUI HUD Text question  (Read 2638 times)

Gorlog

  • Guest
NGUI HUD Text question
« on: June 14, 2013, 08:57:01 AM »
Hello there.

i have a problem with health bar on enemy top. so

code is bellow

  1. var health : int = 50;
  2. var currentHealth:int = 50;
  3. var guiHealth : int = 300;
  4. var ExplosionEffect : Transform;
  5. var Heal_Cube:GameObject;
  6. var KeyCard:GameObject;
  7. var target:Transform;
  8. var KeyCardBoss:boolean = false;
  9. private var draw : boolean=false;
  10. public var myStyle:GUISkin;
  11. public var EnemyString:String;
  12. public var HUD_Drone:GameObject;
  13.  
  14.  
  15.  
  16. public var HealBarSliderEnemy:UISlider;
  17.  
  18.  
  19. var targetPlayer:Transform;
  20.  
  21.  
  22.         function Start () {
  23.                
  24.                 var ComplateMission:GameObject = GameObject.FindGameObjectWithTag("CompleteCount");
  25.                 target = ComplateMission.transform;
  26.                
  27.                 var TargetPL:GameObject = GameObject.FindGameObjectWithTag("PlayerBot");
  28.                 targetPlayer = TargetPL.transform;
  29.                
  30.         }
  31.  
  32.  
  33. function TakeDamage(MachineGunDmg : int)
  34. {
  35.    
  36.         health -= MachineGunDmg;
  37.         draw = true;
  38.         yield WaitForSeconds(1);
  39.         draw = false;  
  40.    
  41.  
  42. }
  43.  
  44.  
  45.  
  46. function Update()
  47. {
  48.    
  49.    
  50.  
  51.    if(health > 0) {
  52.        
  53.          HUD_Drone = GameObject.FindWithTag("HUD_Drone");
  54.          HealBarSliderEnemy = GameObject.FindWithTag("DroneHPBAR").GetComponent(UISlider); // Problem is here i think
  55.                  
  56.          
  57.          HealBarSliderEnemy.foreground.localScale.x =  ( 1.0 * health / currentHealth ) * 200;
  58.         }
  59.   else if( health <=0)
  60.                 {
  61.  
  62.           hover = false;
  63.           health = 0;
  64.           targetPlayer.GetComponent(CursorChange).EnemyCursor = false;
  65.           HealBarSliderEnemy.foreground.localScale.x = 0;
  66.           Destroy(HUD_Drone);
  67.           Destroy(gameObject);
  68.          
  69.       var DropRate: int = Random.Range(1, 4);
  70.       Instantiate(ExplosionEffect,transform.position,transform.rotation);
  71.       target.GetComponent(MissionGates).Counter++;
  72.           if(target.GetComponent(MissionGates).Counter == 36)
  73.           {
  74.           target.collider.isTrigger= true;
  75.        
  76.           }
  77.       if(DropRate == 2)
  78.       {
  79.       Instantiate(Heal_Cube,transform.position,transform.rotation);
  80.       Destroy(gameObject);
  81.       }
  82.      
  83.       if(KeyCardBoss == true)
  84.       {
  85.       Instantiate(KeyCard,transform.position,transform.rotation);
  86.       Destroy(gameObject);
  87.       }
  88.  
  89.      }
  90.  
  91. }





my problem is one enemy is instantiated no problem but 2 enemy same time instantiated or more enemy healtbars wont work im uploading game atm web player

HealBarSliderEnemy = GameObject.FindWithTag("DroneHPBAR").GetComponent(UISlider); // Problem is here i think coz instantiated work but code find it always top of it


Image bellow





all 3 enemy same health value

  1. var health : int = 50;

how can i fix that



game link


https://dl.dropboxusercontent.com/u/32218714/AlphaBetaGridDefender/GridDefender.html
« Last Edit: June 14, 2013, 09:06:01 AM by Gorlog »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI HUD Text question
« Reply #1 on: June 14, 2013, 05:08:02 PM »
Well, yeah. You're using GameObject.Find by tag/name.

First of all -- never use GameObject.Find. Forget that function even exists.

Second, if you have two of them in the scene, you obviously have two objects with the same tag. How do you expect it to find the right one? You need to think about your code a bit more.

Gorlog

  • Guest
Re: NGUI HUD Text question
« Reply #2 on: June 15, 2013, 09:20:48 AM »
thx ArenMook any advice well be nice :)