Search Unity

Problem with Collider2D

Discussion in 'Scripting' started by tarzanno, May 28, 2015.

  1. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    Hi I have a problem with those two scripts. The Sword script triggers when its PolygonCollider2D hits BoxCollider2D of the Enemy and it triggers damageImage.color = flashColour;, but it doesn't make currentHealth shrink. Maybe some fresh look will help?

    EnemyHealth
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class EnemyHealth : MonoBehaviour {
    6.  
    7.     public  int startingHealth = 10;
    8.     public int currentHealthEnemy;
    9.     public Image damageImage;
    10.     public AudioClip deathClip;
    11.     public float flashSpeed = 5f;
    12.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
    13.     public int scoreValue = 10;
    14.    
    15.     Animator anim;
    16.     AudioSource enemyAudio;
    17.     ParticleSystem hitParticles;
    18.     BoxCollider2D boxCollider2D;
    19.     PolygonCollider2D polygonCollider2D;
    20.      public Sword sword;
    21.  
    22.     enemy enemyMobility;
    23.  
    24.     bool isDead;
    25.     bool damaged;
    26.  
    27.    
    28.     void Awake ()
    29.     {
    30.         //Seeting up the references
    31.         sword = GetComponent <PolygonCollider2D>;
    32.         anim = GetComponent <Animator> ();
    33.         enemyAudio = GetComponent <AudioSource> ();
    34.         hitParticles = GetComponentInChildren <ParticleSystem>();
    35.         boxCollider2D = GetComponent <BoxCollider2D> ();
    36.         polygonCollider2D = sword.GetComponent <PolygonCollider2D>();
    37.  
    38.        
    39.        
    40.         //setting up initial health
    41.         currentHealthEnemy = startingHealth;
    42.     }
    43.  
    44.     void OnTriggerEnter2D(Collider2D other)
    45.     {
    46.         if(other.name== "sword")
    47.         {
    48.             damageImage.color = flashColour;
    49.  
    50.         }
    51.         else{
    52.             return;
    53.         }
    54.     }
    55.  
    56.     void Start()
    57.     {
    58.         if (polygonCollider2D.isTrigger == true){
    59.         GameObject go = GameObject.FindWithTag("DamageImage");
    60.         damageImage = go.GetComponent<Image>();
    61.        
    62.         }
    63.         else{
    64.             return;
    65.         }
    66.     }
    67.     void Update()
    68.     {
    69.         //if the enemy has just been damaged...
    70.         if(damaged)
    71.         {
    72.             damageImage.color = flashColour;
    73.        
    74.         }
    75.         else
    76.         {
    77.             damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
    78.         }
    79.  
    80.    
    81.         damaged = false;
    82.     }
    83.    
    84.     public void TakeDamage (int amount)
    85.     {
    86.         damaged = true; //błyskanie
    87.         if(!isDead)
    88.         {
    89.             return;
    90.         }
    91.         enemyAudio.Play ();
    92.         currentHealthEnemy -= amount;
    93.  
    94.         if(currentHealthEnemy <= 0)
    95.         {
    96.             Death ();
    97.         }
    98.     }
    99.    
    100.     void Death ()
    101.     {
    102.         isDead = true;
    103.         //boxCollider2D.isTrigger = true;
    104.  
    105.  
    106.         anim.SetTrigger ("enemyDead");
    107.         enemyAudio.clip = deathClip;
    108.         enemyAudio.Play ();
    109.         Destroy (this.gameObject);
    110.  
    111.        
    112.     }
    113.  
    114. }
    115.  
    Sword:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Sword : MonoBehaviour {
    5.  
    6.     Animator anim;
    7.     GameObject enemy;
    8.     EnemyHealth enemyHealth;
    9.     ScoreManager scoreManager;
    10.     //public int amount = 100;
    11.     bool enemyInRange;
    12.     public int attackDamage = 10;
    13.  
    14.     void Awake()
    15.     {
    16.         anim = enemy.GetComponent<Animator>();
    17.         enemy = GameObject.FindGameObjectWithTag ("Enemy");
    18.         enemyHealth = enemy.GetComponent<EnemyHealth>();
    19.     }
    20.  
    21. void OnTriggerEnter2D(Collider2D enemy)
    22.     {
    23.         if(enemy.tag== "Enemy")
    24.         {
    25.         //Destroy (enemy.gameObject);
    26.             enemyInRange = true;
    27.  
    28.         }
    29.     }
    30.     void OnTriggerExit2D (Collider2D enemy)
    31.     {
    32.         if(enemy.name == "Enemy")
    33.         {
    34.            
    35.             enemyInRange = false;
    36.         }
    37.    
    38.     }
    39.  
    40.     void Update ()
    41.     {
    42.  
    43.         //if the enemy is in range and this enemy is alive...
    44.         if (enemyInRange)
    45.         {
    46.  
    47.             Attack();
    48.         }
    49.        
    50.         //if the enemy has zero or less health///
    51.         if (enemyHealth.currentHealthEnemy <= 0)
    52.         {
    53.             //tell the animator that enemy is dead
    54.             anim.SetTrigger ("enemyDead");
    55.         }
    56.     }
    57.  
    58.     void Attack()
    59.     {
    60.    
    61.        
    62.         //if the enemy has health to lose///
    63.         if (enemyHealth.currentHealthEnemy > 0 && enemyInRange)
    64.         {
    65.             //damage the enemy
    66.        
    67.             enemyHealth.TakeDamage (attackDamage);
    68.         }
    69.        
    70.     }
    71. }
    72.  

    For now, I have this error:
    cannot convert method group 'GetComponent' to non-delegate type 'Sword' consider using parentheses to invoke the method.
    I have no idea what that error means.
    Please help:)
     
  2. Deleted User

    Deleted User

    Guest

    Well, you're assigning a polygon collider to sword:

    sword =GetComponent<PolygonCollider2D>;

    but your sword is of type 'Sword:

    public Sword sword;

    You need to make it of type PolygonCollider2D:

    public PolygonCollider2D sword;
     
  3. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    I changed it, but there is an error, which occured before:

    UnassignedReferenceException: The variable sword of EnemyHealth has not been assigned.
    You probably need to assign the sword variable of the EnemyHealth script in the inspector.


    Of course I made an assignement in the inspector, but I keep getting this error when both colliders touch themselves.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class EnemyHealth : MonoBehaviour {
    6.  
    7.     public  int startingHealth = 10;
    8.     public int currentHealthEnemy;
    9.     public Image damageImage;
    10.     public AudioClip deathClip;
    11.     public float flashSpeed = 5f;
    12.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
    13.     public int scoreValue = 10;
    14.    
    15.     Animator anim;
    16.     AudioSource enemyAudio;
    17.     ParticleSystem hitParticles;
    18.     BoxCollider2D boxCollider2D;
    19.     public PolygonCollider2D sword;
    20.     // public Sword sword;
    21.     PolygonCollider2D polygonCollider2D;
    22.  
    23.     enemy enemyMobility;
    24.  
    25.     bool isDead;
    26.     bool damaged;
    27.  
    28.    
    29.     void Awake ()
    30.     {
    31.         //Seeting up the references
    32.         //sword = GetComponent <PolygonCollider2D>;
    33.         anim = GetComponent <Animator> ();
    34.         enemyAudio = GetComponent <AudioSource> ();
    35.         hitParticles = GetComponentInChildren <ParticleSystem>();
    36.         boxCollider2D = GetComponent <BoxCollider2D> ();
    37.         polygonCollider2D = sword.GetComponent <PolygonCollider2D>();
    38.  
    39.        
    40.        
    41.         //setting up initial health
    42.         currentHealthEnemy = startingHealth;
    43.     }
    44. (...)
    45. }
     
  4. Deleted User

    Deleted User

    Guest

    So the error occurs only when the colliders collide? Can you show your collision code?
     
  5. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    It is inside EnemyHealth and Sword scripts:) Yes, it occurs when they collide. It's weird, I thought everything was written correctly.

    Aslo, there is an error

    NullReferenceException: Object reference not set to an instance of an object
    Sword.Awake () (at Assets/Sword.cs:16)


    on that fragment of code inside Sword.cs:
    Code (CSharp):
    1. void Awake()
    2.     {
    3.         anim = enemy.GetComponent<Animator>();
    But it occurs even if I start the game.
     
  6. Deleted User

    Deleted User

    Guest

    anim = enemy.GetComponent<Animator>();

    enemy is null. I see you do this:

    anim = enemy.GetComponent<Animator>();
    enemy = GameObject.FindGameObjectWithTag ("Enemy");

    You should switch them around like this:

    enemy = GameObject.FindGameObjectWithTag ("Enemy")
    anim = enemy.GetComponent<Animator>();
     
  7. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    Unfortunately, it doesnt work. They are colliding, but right now the blinking of a screen (damageImage.color=flashColour;) is not working as well, so objects are colliding, but there is some problem with EnemyHealth script.
    I guess I should put something in this condition in EnemyHealth script:

    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if(other.name== "sword")
    4.         {
    5.             damageImage.color = flashColour;
    6.  
    7.         }
    8.         else{
    9.             return;
    10.         }
    11.     }
    To be clear: Sword is a child of a Player object and it's PolygonCollider2D is getting enabled in an animation. Maybe that's why the code is not working?
     
  8. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    Okay, for now here is the situation:

    If Sword object is not assigned in the inspector, the OnTriggerEnter2D method is running, but only
    damageImage.color = flashColour;
    works. In this case, when I run the game the EnemyHealth script is automatically being unchecked by Unity.

    When I assign the Sword object in inspector, then the EnemyHealth script is checked, but the damageImage.color=flashColour; is not working.

    In both cases, when I put Destroy (this.gameObject); in OnTriggerEnter2D method, it destroys the object, so colliders are working.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class EnemyHealth : MonoBehaviour {
    6.  
    7.     public  int startingHealth = 10;
    8.     public int currentHealthEnemy;
    9.     public Image damageImage;
    10.     public AudioClip deathClip;
    11.     public float flashSpeed = 5f;
    12.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
    13.     public int scoreValue = 10;
    14.  
    15.     Animator anim;
    16.     AudioSource enemyAudio;
    17.     ParticleSystem hitParticles;
    18.     BoxCollider2D boxCollider2D;
    19.  
    20.     public Sword sword;
    21.  
    22.     PolygonCollider2D polygonCollider2D;
    23.  
    24.  
    25.     GameObject enemy;
    26.     bool isDead;
    27.     bool damaged;
    28.  
    29.  
    30.     void Awake ()
    31.     {
    32.         //Seeting up the references
    33.    
    34.         enemy = GameObject.FindGameObjectWithTag ("Enemy");
    35.         boxCollider2D = GetComponent <BoxCollider2D> ();
    36.  
    37.         polygonCollider2D = sword.GetComponent <PolygonCollider2D>();
    38.  
    39.         anim = GetComponent <Animator> ();
    40.         enemyAudio = GetComponent <AudioSource> ();
    41.         hitParticles = GetComponentInChildren <ParticleSystem>();
    42.      
    43.      
    44.         //setting up initial health
    45.         currentHealthEnemy = startingHealth;
    46.     }
    47.  
    48.     void OnTriggerEnter2D(Collider2D other)
    49.     {
    50.         if(other.name== "sword")
    51.         {
    52.             damageImage.color = flashColour;
    53.             damaged=true;
    54.             //Destroy (this.gameObject);
    55.  
    56.  
    57.         }
    58.         else{
    59.             return;
    60.         }
    61.     }
    62.  
    63.     void Start()
    64.     {
    65.         if (boxCollider2D.isTrigger == true){
    66.         GameObject go = GameObject.FindWithTag("DamageImage");
    67.         damageImage = go.GetComponent<Image>();
    68.      
    69.         }
    70.         else{
    71.             return;
    72.         }
    73.     }
    74.     void Update()
    75.     {
    76.         //if the enemy has just been damaged...
    77.         if(damaged)
    78.         {
    79.             damageImage.color = flashColour;
    80.  
    81.      
    82.         }
    83.         else
    84.         {
    85.             damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
    86.         }
    87.  
    88.  
    89.         damaged = false;
    90.     }
    91.  
    92.     public void TakeDamage (int amount)
    93.     {
    94.         damaged = true; //błyskanie
    95.         currentHealthEnemy -= amount;
    96.         /*if(!isDead)
    97.         {
    98.             return;
    99.         }*/
    100.         enemyAudio.Play ();
    101.  
    102.  
    103.         if(currentHealthEnemy <= 0 && isDead)
    104.         {
    105.             Death ();
    106.         }
    107.     }
    108.  
    109.     void Death ()
    110.     {
    111.         isDead = true;
    112.         //boxCollider2D.isTrigger = true;
    113.  
    114.  
    115.         anim.SetTrigger ("enemyDead");
    116.         enemyAudio.clip = deathClip;
    117.         enemyAudio.Play ();
    118.         Destroy (enemy.gameObject);
    119.  
    120.      
    121.     }
    122.  
    123. }
    124.  
    Sword:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Sword : MonoBehaviour {
    5.  
    6.     Animator anim;
    7.     GameObject enemy;
    8.     EnemyHealth enemyHealth;
    9.     ScoreManager scoreManager;
    10.     bool enemyInRange;
    11.     public int attackDamage = 10;
    12.  
    13.     void Awake()
    14.     {
    15.         enemy = GameObject.FindGameObjectWithTag ("Enemy");
    16.         enemyHealth = enemy.GetComponent<EnemyHealth>();
    17.         anim = enemy.GetComponent<Animator>();
    18.     }
    19.  
    20. void OnTriggerEnter2D(Collider2D enemy)
    21.     {
    22.         if(enemy.tag== "Enemy")
    23.         {
    24.      
    25.             enemyInRange = true;
    26.  
    27.         }
    28.     }
    29.     void OnTriggerExit2D (Collider2D enemy)
    30.     {
    31.         if(enemy.name == "Enemy")
    32.         {
    33.          
    34.             enemyInRange = false;
    35.         }
    36.  
    37.     }
    38.  
    39.     void Update ()
    40.     {
    41.  
    42.         //if the enemy is in range and this enemy is alive...
    43.         if (enemyInRange)
    44.         {
    45.  
    46.             Attack();
    47.         }
    48.      
    49.         //if the enemy has zero or less health///
    50.         if (enemyHealth.currentHealthEnemy <= 0)
    51.         {
    52.             //tell the animator that enemy is dead
    53.             anim.SetTrigger ("enemyDead");
    54.         }
    55.     }
    56.  
    57.     public void Attack()
    58.     {
    59.  
    60.      
    61.         //if the enemy has health to lose///
    62.         if (enemyHealth.currentHealthEnemy >= 0)
    63.         {
    64.             //damage the enemy
    65.      
    66.             enemyHealth.TakeDamage (attackDamage);
    67.         }
    68.      
    69.     }
    70. }
    71.  
    Also, I'm getting
    "NullReferenceException: Object reference not set to an instance of an object"
    on "polygonCollider2D = sword.GetComponent <PolygonCollider2D>();" in EnemyHealth

    and on
    Code (CSharp):
    1.     public void Attack()
    2.     {
    3.  
    4.      
    5.         //if the enemy has health to lose///
    6.         if (enemyHealth.currentHealthEnemy >= 0)
    in Sword script, when colliders collide
     
    Last edited: May 29, 2015
  9. Deleted User

    Deleted User

    Guest

    I think it's because of this code in your Awake method of Sword script:
    1. enemy = GameObject.FindGameObjectWithTag ("Enemy");
    2. enemyHealth = enemy.GetComponent<EnemyHealth>();
    Either:
    1) It can't find any GameObject with tag "Enemy"
    2) enemy does not have an EnemyHealth script attached to it.

    Check to see your enemy is really tagged as "Enemy" and is not disabled when the game starts. Also make sure it has an EnemyHealth component.
     
  10. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    The Enemy object is presented on a screen at the same time Player with Sword object (when game starts, they are already on a screen). Enemy is tagged as Enemy and has Enemy Health Script attached to it.
    If I don't attach any object in Inspector to the Enemy Health (Sword), then when game starts, it is disabled. When I attach object Sword, it starts checked.
    Sword object is checked as visible all the time, but it's Polygon Collider 2D is being enabled in Animator. Maybe this is a problem? I am struggling with script-animator triggers right now, so maybe animations are preventing the script Sword and EnemyHealth from running properly?

    this is what it looks like:
     
  11. Deleted User

    Deleted User

    Guest

    ”polygonCollider2D = sword.GetComponent<PolygonCollider2D>();”

    Are you assigning sword in the inspector?
     
  12. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    You mean in the script EnemyHealth in inspector? Yes, I do. I attached a screenshot in previous post. Is this what you mean?
     
  13. Deleted User

    Deleted User

    Guest

    No can you post a screenshot of Sword?
     
  14. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58

    This is it. Polygon Collider 2 is being enabled in the animator.
     
  15. Deleted User

    Deleted User

    Guest

    I think it's because your PolygonCollider2D is disabled. Try enabling it and see if that fixes your NullReference error.
     
  16. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    But as I said before, Polygon Collider 2D is getting enabled during the animation, look:

    And, of course, enabling it, besides the animation, doesn't change anything.
     
  17. Deleted User

    Deleted User

    Guest

    Yes, but Awake runs before the animation so at the time Awake runs it will be disabled and GetComponent will not get it because it's disabled.
     
  18. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    I moved
    polygonCollider2D = sword.GetComponent <PolygonCollider2D>();
    from Awake() to Update()
    but there is the same error.

    I put this line of code where it was before, and I run the game again. EnemyHealth.currenthealthEnemy is shrinking now, as the sword collides with it.
    Now I have to learn how to make trigger "anim.SetTrigger ("enemyDead");" from void Death() work, because "Destroy(enemy.gameObject);" that is present in a code after the anim.SetTrigger doesn't run. When it is before SetTrigger, it works.

    Thank you very much for helping me. I appreciate it.

    PS: The bad thing is, I don't know what was the problem here.;)
     
  19. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    It was a while, and the problem still occurs.
    When the Sword collides with Enemy object, everything works fine.
    But when it comes to a prefab made of the Enemy object, the health parameter is not shrinking, like the sword collider is not colliding with enemyprefab object (but it is, there is a screen light blinking and particles system working).
    I made sure that I find every object with tag, not the name (because of "clone" in prefab names). I can't find the fault in code (or maybe the problem is somewhere else?)

    This is the sword script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Sword : MonoBehaviour {
    5.  
    6.     Animator anim;
    7.     GameObject enemy;
    8.     EnemyHealth enemyHealth;
    9.     ScoreManager scoreManager;
    10.     //public int amount = 100;
    11.     bool enemyInRange;
    12.     public int attackDamage = 10;
    13.  
    14.  
    15.     void Awake()
    16.     {
    17.  
    18.         enemy = GameObject.FindGameObjectWithTag ("Enemy").gameObject;
    19.         enemyHealth = enemy.GetComponent<EnemyHealth>();
    20.         anim = enemy.GetComponent<Animator>();
    21.  
    22.  
    23.  
    24.     }
    25.  
    26. void OnTriggerEnter2D(Collider2D enemy)
    27.     {
    28.         if(enemy.tag== "Enemy")
    29.         {
    30.         //Destroy (enemy.gameObject);
    31.             enemyInRange = true;
    32.  
    33.         }
    34.     }
    35.     void OnTriggerExit2D (Collider2D enemy)
    36.     {
    37.         if(enemy.tag == "Enemy")
    38.         {
    39.          
    40.             enemyInRange = false;
    41.         }
    42.  
    43.     }
    44.  
    45.     void Update ()
    46.     {
    47.  
    48.         //if the enemy is in range and this enemy is alive...
    49.         if (enemyInRange)
    50.         {
    51.  
    52.             Attack();
    53.         }
    54.         /*
    55.         //if the enemy has zero or less health///
    56.         if (enemyHealth.currentHealthEnemy <= 0)
    57.         {
    58.             //tell the animator that enemy is dead
    59.      
    60.             anim.SetTrigger ("enemyDead");
    61.  
    62.         }*/
    63.     }
    64.  
    65.     public void Attack()
    66.     {
    67.  
    68.  
    69.         //if the enemy has health to lose///
    70.         if (enemyHealth.currentHealthEnemy >= 0)
    71.         {
    72.             //damage the enemy
    73.  
    74.             enemyHealth.TakeDamage (attackDamage);
    75.         }
    76.         else {
    77.             return;
    78.         }
    79.      
    80.     }
    81. }
    82.  
    I tried to move everything from Awake() to Update(), as sword script should find references to enemy clone objects (prefabs) as they are spawning, but when I do it, nothing is spawning any more. I don't get eny errors and I have no idea why it is happening.

    The EnemyHealth script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class EnemyHealth : MonoBehaviour {
    6.  
    7.     public  int startingHealth = 10;
    8.     public int currentHealthEnemy;
    9.     public Image damageImage;
    10.     public AudioClip deathClip;
    11.     public float flashSpeed = 5f;
    12.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
    13.     public int scoreValue = 10;
    14.  
    15.     AnimationClip clip;
    16.     Animator anim;
    17.     AudioSource enemyAudio;
    18.     public GameObject hitParticles;
    19.     BoxCollider2D boxCollider2D;
    20.     //public Sword sword;
    21.     public Transform sword;
    22.     PolygonCollider2D polygonCollider2D;
    23. //    public Enemy enemy;
    24.     //enemy enemyMobility;
    25.     GameObject enemy;
    26.     bool isDead;
    27.     bool damaged;
    28.     private Animator animator;
    29.  
    30.  
    31.     void Awake ()
    32.     {
    33.         //Seeting up the references
    34.         //sword = GetComponent <PolygonCollider2D>;
    35.         enemy = GameObject.FindGameObjectWithTag ("Enemy");
    36.         boxCollider2D = GetComponent <BoxCollider2D> ();
    37.         currentHealthEnemy = startingHealth;
    38.  
    39.  
    40.         anim = GetComponent <Animator> ();         //można przenieść do start()
    41.         enemyAudio = GetComponent <AudioSource> ();
    42.         //hitParticles = GetComponentInChildren <ParticleSystem>();
    43.      
    44.      
    45.  
    46.     }
    47.  
    48.     void OnTriggerEnter2D(Collider2D other)
    49.     {
    50.         if(other.tag== "Sword")
    51.         {
    52.             polygonCollider2D = sword.GetComponent <PolygonCollider2D>();
    53.             damageImage.color = flashColour;
    54.             damaged=true;
    55.             Instantiate (hitParticles, enemy.transform.position,Quaternion.Euler(0, 180,0 ));
    56.  
    57.             //Destroy (this.gameObject);
    58.             //return;
    59.  
    60.         }
    61.  
    62.         else{
    63.             return;
    64.         }
    65.     }
    66.  
    67.     void Start()
    68.     {
    69.         //setting up initial health
    70.         sword = GameObject.FindWithTag("Sword").transform;
    71.         Debug.Log(enemy);
    72.         Debug.Log(this);
    73.         if (boxCollider2D.isTrigger == true){
    74.         GameObject go = GameObject.FindWithTag("DamageImage");
    75.         damageImage = go.GetComponent<Image>();
    76.             isDead = false;
    77.         }
    78.         else{
    79.             return;
    80.         }
    81.  
    82.     }
    83.     void Update()
    84.     {
    85.  
    86.  
    87.         //if the enemy has just been damaged...
    88.         if(damaged)
    89.         {
    90.             damageImage.color = flashColour;
    91.  
    92.      
    93.         }
    94.  
    95.         else
    96.         {
    97.             damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
    98.         }
    99.  
    100.  
    101.         damaged = false;
    102.  
    103.  
    104.     }
    105.  
    106.     public void TakeDamage (int amount)
    107.     {
    108.         damaged = true; //błyskanie
    109.  
    110.         currentHealthEnemy -= amount;
    111.         enemyAudio.Play ();
    112.             if(currentHealthEnemy <= 0 && !isDead)
    113.         {
    114.             Death ();
    115.         }
    116.     }
    117.  
    118.     void Death ()
    119.     {
    120.  
    121.         isDead = true;
    122.         //boxCollider2D.isTrigger = true;
    123.  
    124. //        Animation.Play(clip.name);
    125.         anim.SetTrigger ("enemyDead");
    126.         //transform.GetComponent<Animation>().Play("enemyDead");
    127.         enemyAudio.clip = deathClip;
    128.         enemyAudio.Play ();
    129.         Destroy (this.gameObject);
    130.  
    131.  
    132.      
    133.     }
    134.     /*
    135.     public Animator GetAnimator()
    136.     {
    137.         return animator;
    138.     }*/
    139.  
    140. }
    141.  
     
    Last edited: Jun 24, 2015
  20. Pharan

    Pharan

    Joined:
    Oct 28, 2013
    Posts:
    102
    Confusing.

    Is there only one enemy and that's the only enemy that will ever exist and exists at the start of the scene?
    Is that how lines 18 and 20 are always the correct references?
     
    Last edited: Jun 24, 2015
  21. tarzanno

    tarzanno

    Joined:
    Apr 23, 2015
    Posts:
    58
    No, of course not. There is an object, which spawns up to 10 "Enemy" objects from a prefab which have EnemyHealth script attached.
    I guess I have to use
    enemy = GameObject.FindGameObjectsWithTag ("Enemy");
    and use a reference to multiple objects. Am I heading in the right direction, searching how to make a table (Array?) of enemy clones objects?