Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiate & GettingComponents error [Video inside]

Discussion in 'Scripting' started by Zackhanzo, Jul 25, 2014.

  1. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Hi guys, i just wanna ask you something. I have 1 Zombie with a navmesh agent component, which works great. But when i try using it in the spawner something weird happens. I cant see its colliders, or the gameobject get destroyed without taking the proper animation...

    The spawner script is the same, that i was using without the navmesh agent component and it works great. Is any kind of Unity bug or so?

    Script:
    Code (CSharp):
    1. public class followandhit : MonoBehaviour {
    2.     //public Transform m_player;
    3.     Animator animador;
    4.     Transform myTransfrom;
    5.     Transform target;
    6.     bool camBlood;
    7.     public float damage;
    8.     public GameObject blood;
    9.     //
    10.     public float hitPoints=100f;
    11.     public float currentHitPoints;
    12.     public GameObject Blood;
    13.  
    14.     // Use this for initialization
    15.     void Awake(){
    16.  
    17.         myTransfrom = gameObject.transform;
    18.         target = GameObject.FindWithTag("Player").transform;
    19.  
    20.         }
    21.  
    22.     void Start () {
    23.  
    24.         animador = GetComponent<Animator> ();
    25.         currentHitPoints = hitPoints + Time.deltaTime/2;
    26.  
    27.  
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update () {
    32.         camBlood = false;
    33.         GetComponent<NavMeshAgent> ().destination = target.position;
    34.         if (Vector3.Distance(target.position, myTransfrom.position)>1.7)
    35.                {
    36.                         animador.SetBool ("Follow", true);
    37.                         animador.SetBool("Hit",false);
    38.                 }
    39.         if (Vector3.Distance (target.position, gameObject.transform.position) < 1.7)
    40.                 {
    41.             animador.SetBool ("Follow",false);
    42.             animador.SetBool ("Hit",true);
    43.  
    44.  
    45.                 }
    46.  
    47.  
    48.     }
    49.  
    50.     void OnCollisionEnter(Collision collision)
    51.     {
    52.         if (collision.gameObject.tag=="Player") {
    53.             camBlood=true;
    54.             healthPlayer h;
    55.             h = collision.transform.GetComponent<healthPlayer> ();
    56.             h.TakeDamagePlayer (damage);
    57.             Instantiate (blood, collision.transform.position, collision.transform.rotation);
    58.        
    59.        
    60.         }
    61.     }
    62.  
    63.     public void TakeDamage(float amt){
    64.  
    65.         currentHitPoints -= amt;
    66.         animador.SetBool ("hit", true);
    67.         if (currentHitPoints <= 0) {
    68.  
    69.             animador.SetBool("Dead",true);
    70.             animador.SetBool ("Follow",false);
    71.             Destroy (gameObject,3);    
    72.         }
    73.     }
    74.  
    75. }

    And the spawner script:


    Code (CSharp):
    1. public class SpawnZombie1 : MonoBehaviour {
    2.  
    3.     public GameObject zombie1;
    4.     public float timeToSpawn;
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.         InvokeRepeating ("Spawn", 10, timeToSpawn);
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.  
    16.  
    17.  
    18.  
    19.  
    20.     }
    21.  
    22.     void Spawn()
    23.     {
    24.  
    25.      Instantiate (zombie1, transform.position, transform.rotation) ;
    26.                          
    27.        
    28.     }
    29.  
    30.  
    31. }
    32.  

    The Zombie spawns great, and he tries to chasing me. But when i shoot to it, some things weird just happen. I have a pistol with a laser which shows me the colliders of the zombie, and sometimes i cant see them properly, or the zombie dont get the value "true" on "dead" and get destroyed without animation...

    Its weird because the non-instantiated Zombie works fine. And the script of the spawner is the same that i was using 1 day ago without the navmesh agent and it worked good.
     
    Last edited: Jul 25, 2014
  2. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Well i tried putting in the scene tons of Zombies deactivating them and activating by a simple timer and it works good too. But i still cant make this works with the spawner ( I think its something weird due to the prefab with Navmesh Agent attached). Tried again, and i hit the enemy 3 times (health=100 damage=40) and some zombies dont get "Dead" animation and just destroy themselves after 3 seconds and other Zombies get the "Dead" animation but after time... like 1 sec after my ray impact on their collider...Not inmediately. I saw my scripts to see if this could be a cache problem, but the scripts are ok!

    Im a little stressed right now :(
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    why are you getting the navmeshagent in update? surely it's only going to ever have one, so that should be in Start()?
     
    Zackhanzo likes this.
  4. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    It has to be on Update() to see on each frame where is the player position

    Ill upload a utube vid better
     
    Last edited: Jul 25, 2014
  5. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Following with my own X-File. I recorded this video (no adds):


    You can see:

    1.First two zombies---> Works fine (no spawn, already in the scene)
    2.Next Zombies ---> Works bad. They detect the hitpoint, and the damage (they die after 3 hits) and the navmesh stopped. So if i had this on my shooting stuff, in the raycast if player shoots gameobject with tag "Enemy".

    Code (CSharp):
    1. followandhit h;
    2.                 h = hit.transform.GetComponent<followandhit> ();
    3.                 h.TakeDamage (damage);
    And the public void TakeDamage is:


    Code (CSharp):
    1. public void TakeDamage(float amt){
    2.  
    3.         currentHitPoints -= amt;
    4.  
    5.         if (currentHitPoints <= 0) {
    6.             navmesh.enabled=false;
    7.             animador.SetBool("Dead",true);
    8.             animador.SetBool ("Follow",false);
    9.             Destroy (gameObject,3);      
    10.         }
    11.     }
    Why are they taking 1ºst line, pass through the condition and take all the instructions good (navmesh disabled and destroy) and dont get the bool values of the animador <Animator>();?
     
  6. Zackhanzo

    Zackhanzo

    Joined:
    Jun 5, 2014
    Posts:
    65
    Well one day later i get why is getting an error in here. Well... animador is getting the components from the Animator of the gameObject, and works good. But when i instantiated the new "Zombie1(Clone)" isnt getting the components from the initial Animator.

    I though if i put this on my "followandhit" script....

    animador=GameObject.FindGameObjectWithTag("Enemy").GetComponents<Animator>();

    will resolve all of this, but it isnt.....(Both, have the same tag of course)

    So... I know where is the problem, but i dont get how to solve it. Anyone?