Search Unity

Script doesn't work on duplicates.

Discussion in 'Scripting' started by Xaos95, May 25, 2017.

  1. Xaos95

    Xaos95

    Joined:
    Nov 23, 2016
    Posts:
    2
    So, i wanted to make an zombie survival game with infinite waves and i have made an attack system where if i enter an certain collider on the enemy, it will play its attack animation and it's hitbox will be activated. Now the problem is that when i duplicate the enemy the script or function (not sure) only works on the main enemy, not the copy. And whenever i destroy the "main" enemy i get an error leading to the attack area script. The prefab is exactly the same and i used the instantiate function. If you don't understand my problem yet, take a look at this video i made :


    As you can see whenever i enter any of the duplicates "attack area" the animation only plays on the main prefab.

    Here is the Attack Area script:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class AttackArea : MonoBehaviour {
    5.  
    6.  
    7.     Animator animator;
    8.     DetectHit detectHit;
    9.  
    10.     Collider collider;
    11.  
    12.     void Start ()
    13.     {
    14.         animator = GameObject.FindGameObjectWithTag ("Enemy").GetComponent<Animator> ();
    15.         detectHit = GameObject.FindGameObjectWithTag ("Hitbox").GetComponent<DetectHit> ();
    16.  
    17.         collider = GetComponent<Collider> ();
    18.    
    19.     }
    20.    
    21.     void Update ()
    22.     {
    23.        
    24.     }
    25.  
    26.  
    27.     void OnTriggerEnter(Collider collider)
    28.     {
    29.  
    30.         detectHit.enabled = true;
    31.  
    32.         if (collider.tag == "Player")
    33.         {
    34.             detectHit.enabled = true;
    35.             animator.SetBool ("isAttacking", true);
    36.             animator.SetBool ("isWalking", false);
    37.             animator.SetBool ("isDead", false);
    38.         }
    39.     }
    40.  
    41.     void OnTriggerExit (Collider col)
    42.     {
    43.         if (col.tag == "Player")
    44.         {
    45.             detectHit.enabled = false;
    46.  
    47.             animator.SetBool ("isAttacking", false);
    48.             animator.SetBool ("isWalking", true);
    49.             animator.SetBool ("isDead", false);
    50.         }
    51.     }
    52.  
    53. }

    Any kind of help will be appreciated, thank you.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    When you find the object with tag, it returns 1 object. Just 1.
    If the enemy and areas are duplicated, you should make those variables specific (local) for the script, and look them up with GetComponent in start. That way they're cached and refer to that specific enemy/hitbox/whatever.
     
  3. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry, yes, I should have included that in my answer. I was going to be, but kinda forgot. Those are good to know about, but if the script is supposed to work how I imagine it is, the local ones (variables) will be preferable. These look like they're on the enemy, so you wouldn't want to find a list of enemies or hitboxes (I imagine) :)
     
    cstooch likes this.
  5. Xaos95

    Xaos95

    Joined:
    Nov 23, 2016
    Posts:
    2
    Could you show an example of some sort, i'm not sure if i understand and do i have to do this in the attackarea script or the spawner script or both of them.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Finding objects with tag is going to lead you in circles. It's always going to give you a global list of all of them, which means you're going to have to do a hell of a lot of work to figure out which is the right one.

    The tag system is a bit of a dead end in the long run anyway, since you can only have one tag per object. If you use the tag system for more than one purpose, they're eventually going to collide, and you'll have to rewrite it anyway.

    Instead, you can use GetComponentInChildren<DetectHit>() or GetComponentInParent<DetectHit>(), which will crawl down or up the hierarchy respectively to find that component in the object.

    You can also just make the objects public and drag in the reference in the inspector.
     
    LiterallyJeff likes this.