Search Unity

Animation not stopping?

Discussion in 'Animation' started by JobenTan, Apr 6, 2018.

  1. JobenTan

    JobenTan

    Joined:
    Mar 6, 2018
    Posts:
    6
    It only works when I call it from another script. But I don't want that. Been figuring around with it but it just won't work. Any idea why or what's wrong with my code?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Deathscript : MonoBehaviour
    6. {
    7.     public bool Alive;
    8.     public Rigidbody[] rb;
    9.     private int j;
    10.     // Use this for initialization
    11.     void Start()
    12.     {
    13.         Alive = true;
    14.         for (j = 0; j < 11; j++)
    15.         {
    16.             rb[j].isKinematic = true;
    17.         }
    18.  
    19.     }
    20.     void OnCollisionEnter(Collision collision)
    21.     {
    22.         if (collision.gameObject.name == "Weapon")
    23.         {
    24.             Alive = false;
    25.             for (j = 0; j < 11; j++)
    26.             {
    27.                 rb[j].isKinematic = false;
    28.             }
    29.         }
    30.     }
    31.     private void LateUpdate()
    32.     {
    33.         if (Alive == false)
    34.         {
    35.             GetComponent<Animator>().enabled = false;
    36.         }
    37.     }
    38. }
     
  2. Kev00

    Kev00

    Joined:
    Dec 6, 2016
    Posts:
    229
    put a debug line before if (collision.gameObject.name == "Weapon") and first make sure it's not being called.

    If it is being called, does the weapon have child objects that are not named "Weapon"?
     
  3. JobenTan

    JobenTan

    Joined:
    Mar 6, 2018
    Posts:
    6
    Hi thanks for the reply. But still does not work. Tried the debugging and it is not called at all until collided. Once one character collided to the "Weapon" object, it will stop all the animations from other different characters. Still couldn't figure out why. I even tried adding in the gameobject to specify the character in this code but still no vail.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Deathscript : MonoBehaviour
    6. {
    7.     public GameObject student;
    8.     public static bool Alive;
    9.     public Rigidbody[] rb;
    10.     private int j;
    11.     // Use this for initialization
    12.     void Start()
    13.     {
    14.         Alive = true;
    15.         for (j = 0; j < 11; j++)
    16.         {
    17.             rb[j].isKinematic = true;
    18.         }
    19.  
    20.     }
    21.     void OnCollisionEnter(Collision collision)
    22.     {
    23.         if (collision.gameObject.name == "Weapon")
    24.         {
    25.             Alive = false;
    26.             for (j = 0; j < 11; j++)
    27.             {
    28.                 rb[j].isKinematic = false;
    29.             }
    30.         }
    31.     }
    32.     void Update()
    33.     {
    34.         if (Alive == false)
    35.         {
    36.             student.GetComponent<Animator>().enabled = false;
    37.         }
    38.     }
    39. }
     
  4. JobenTan

    JobenTan

    Joined:
    Mar 6, 2018
    Posts:
    6
    And another thing I found when I put debug at the line:
    Code (CSharp):
    1.             Debug.Log(student.GetComponent<Animator>().enabled = false);
    It shows in console the numbers beside the error keeps going up. What could that be?
     
  5. JobenTan

    JobenTan

    Joined:
    Mar 6, 2018
    Posts:
    6
    Ok nvm I solve the problem due to a small mistake =_=||.

    I didn't notice this line was public:
    Code (CSharp):
    1. public static bool Alive;
    And changed to:
    Code (CSharp):
    1. private bool Alive;
    works.