Search Unity

Static vars and multiple enemies

Discussion in 'Scripting' started by Desconocido, May 3, 2011.

  1. Desconocido

    Desconocido

    Joined:
    May 1, 2011
    Posts:
    13
    I have an enemy script, which has the enemy health, ai and everything in one script, the script works like this, if life goes to 0 my object enemy die ...But the problem with it is that whenever one enemy is deleted all of them are deleted. They all have same script attached to them and they all have same tag. I read that I should not use static vars and use GetComponent, but im noob and I can¡'t understand how to use that method, can hel me please?

    Thx in advance!
     
  2. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    Could you show some code?

    If you have something like this:

    Code (csharp):
    1. static int Health;
    then its understandable why all your enemys will die. A static field/property instance is shared between all Instances of the class which contains the static field/property. You dont want this, you want a separate variable and not a shared one. Just remove the static and be happy.

    Only relevant if you have after removing the static keyword errors when accessing Health:
    Find your component, cast the component to the type of your component (oh yeah, sounds badass, huh?) and access the health:

    Code (csharp):
    1. var enemy = myEnemyGameObject.GetComponent<Enemy>();
    2. enemy.Health -= damage;
     
  3. Desconocido

    Desconocido

    Joined:
    May 1, 2011
    Posts:
    13
    yes. i use:

    Code (csharp):
    1. static var Health = 100;
    in the enemy script, and in other script use

    Code (csharp):
    1. ENEMYSCRIPT.Health -=10
    to subtract health ... all in JS ...
     
    Last edited: May 3, 2011
  4. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    Ok, great, then you can follow my last post as it is 100% relevant for your problem.
     
  5. Desconocido

    Desconocido

    Joined:
    May 1, 2011
    Posts:
    13
    Have some probleams, look, in the enemy script i put:

    Code (csharp):
    1. var vida = 100;
    And in the impact bullet script:

    Code (csharp):
    1. function Update()
    2. {
    3.     var dir = transform.TransformDirection(Vector3.forward);
    4.     var hit : RaycastHit;
    5.    
    6.     Debug.DrawRay(transform.position, dir * 0.5, Color.red);
    7.    
    8.     if (Physics.Raycast(transform.position, dir, hit, 0.5))
    9.     {
    10.         if (hit.collider.gameObject.tag == "Enemy")
    11.         {
    12.             var enemy = myEnemyGameObject.GetComponent<Enemy>();
    13.             enemy.Health -= 10;
    14.                         Destroy(gameObject);
    15.         }
    16.     }
    17. }
    but doest work .. have patience im noob :(
     
  6. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    If its not working, what exaclty is it doing? Errors, eg.?

    Also, your health variable is called vida, but you substract 10 from Health, how should this work?
     
  7. Desconocido

    Desconocido

    Joined:
    May 1, 2011
    Posts:
    13
    :yap but all call health, vida is in spanish :p but all have same name ... errors in line

    Code (csharp):
    1. var enemy = myEnemyGameObject.GetComponent<Enemy>();
     
  8. BigShedMedia

    BigShedMedia

    Joined:
    May 2, 2011
    Posts:
    10
    Code (csharp):
    1. var enemy = myEnemyGameObject.GetComponent<Enemy>();
    This is wrong because you are using <Enemy>();

    It would need to be like this:

    Code (csharp):
    1. var enemy = myEnemyGameObject.GetComponent(Enemy);
     
  9. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Actually his method is fine, he's using the generic GetComponent... the only problem is he forgot a period.

    Code (csharp):
    1. var enemy = myEnemyGameObject.GetComponent<Enemy>();
    should be

    Code (csharp):
    1. var enemy = myEnemyGameObject.GetComponent.<Enemy>();
    However, a separate issue entirely is that you don't appear to be defining myEnemyGameObject anywhere; I think you took Marrrk's example a bit too literally. What you probably want is:

    Code (csharp):
    1.  
    2. if (hit.collider.gameObject.tag == "Enemy")
    3. {
    4.     var enemy = hit.collider.gameObject.GetComponent.<Enemy>();
    5.     enemy.Health -= 10;
    6.         Destroy(gameObject);
    7. }
    8.  
    Assuming your enemy health/status script is called "Enemy.js", and the public member variable in "Enemy.js" is named "Health" (not vida, Unity doesn't intuitively translate Spanish, hehe).
     
  10. Desconocido

    Desconocido

    Joined:
    May 1, 2011
    Posts:
    13
    yes. i put this:

    Code (csharp):
    1. var enemy = SCRIPRENEMYHERE.GetComponent(enemy);
    2. enemy.health -= 10;
    Error

    Code (csharp):
    1. BCE0005: Unknown identifier: 'enemy'.
     
  11. Desconocido

    Desconocido

    Joined:
    May 1, 2011
    Posts:
    13
    haha yap is Health ... but have this error:

    BCE0138: 'quack' is not a generic definition.
     
  12. ahmetDP_1

    ahmetDP_1

    Joined:
    Sep 23, 2010
    Posts:
    113
    This way it should work:

    Enemy.js
    =========
    Code (csharp):
    1. var health : int = 100;
    HitController.js
    =========
    Code (csharp):
    1. var myEnemyGameObject : GameObject;
    2.  
    3. // Use this for initialization
    4. function Start ()
    5. {
    6. // You should define this variable first.
    7.     myEnemyGameObject = GameObject.Find("Enemy");
    8. }
    9.  
    10. function Update ()
    11. {
    12.     var dir = transform.TransformDirection(Vector3.forward);   
    13.     var hit : RaycastHit;
    14.    
    15.     Debug.DrawRay(transform.position, dir * 0.5, Color.red);
    16.    
    17.     if (Physics.Raycast(transform.position, dir, hit, 0.5))
    18.     {
    19.         if (hit.collider.gameObject.tag == "Enemy")
    20.         {
    21.             // Check out the documentation for GetComponent method
    22.             //var enemy = myEnemyGameObject.GetComponent<Enemy>();  // works in c# : function GetComponent (type : Type) : Component
    23.             var enemy = myEnemyGameObject.GetComponent(Enemy);      // works in js : function GetComponent (type : Type) : Component
    24.             enemy.Health -= 10;
    25.             Destroy(gameObject);
    26.         }
    27.     }
    28. }
    and the link to doc : http://unity3d.com/support/documentation/ScriptReference/Component.GetComponent.html
     
  13. Desconocido

    Desconocido

    Joined:
    May 1, 2011
    Posts:
    13
    Thx ahmetDP , but have a question, in the line:

    Code (csharp):
    1. myEnemyGameObject = GameObject.Find("Enemy");
    have this error:

    Code (csharp):
    1. BCE0005: Unknown identifier: 'myEnemyGameObject'.
    you can u explain me why?
     
  14. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    Maybe I can, you copied the script wrong, scroll up inside the HitController.js script of ahmetD´s post and look at the bright and epic line which contains:

    Code (csharp):
    1. var myEnemyGameObject : GameObject;
     
  15. Desconocido

    Desconocido

    Joined:
    May 1, 2011
    Posts:
    13
    ohh my fault ... now can kill the enemies ... but for example, i have two enemies, one in right corner and one in left corner, if i kill the right corner, die perfectly, if i kill the left corner, die perfectly ... BUT if i kill the left enemy first, die the right corner enemy hahaha
     
  16. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    You could remove the myEnemyGameObject stuff and replace the following line:

    Code (csharp):
    1. var enemy = myEnemyGameObject.GetComponent(Enemy);
    with:
    Code (csharp):
    1. var enemy = hit.collider.gameObject.GetComponent(Enemy);
     
    Last edited: May 3, 2011
  17. Desconocido

    Desconocido

    Joined:
    May 1, 2011
    Posts:
    13
    finally its work!, thank you so much guys!
     
  18. ahmetDP_1

    ahmetDP_1

    Joined:
    Sep 23, 2010
    Posts:
    113
    Yepp. That's it.

    "bright and epic line" :) nice and kind saying. I liked it.