Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Animator.SetInteger not working?

Discussion in 'Scripting' started by GamesOnAcid, Oct 19, 2016.

  1. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    280
    Just got started with Mecanim, and it's a lot less intimidating then I originally thought. The first problem I encountered here is with this code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NightHawkAndo : MonoBehaviour {
    5.    
    6.     public PlayerController player;
    7.     public float damageGiven;
    8.     public float damageToPlayer;
    9.     public float health;
    10.     public float firerate;
    11.     public float moveSpeed;
    12.     public GameObject bossDoor;
    13.     public bool startFight;
    14.     public Animator animator;
    15.  
    16.     void Start() {
    17.         player = GameObject.Find ("player").GetComponent<PlayerController> ();
    18.         moveSpeed = 6;
    19.         health = 250;
    20.         firerate = 5;
    21.         bossDoor = GameObject.Find ("bossDoor");
    22.         bossDoor.SetActive (false);
    23.         animator = GetComponent<Animator> ();
    24.         startFight = true;
    25.         animator.SetInteger ("bossAttack", -2);
    26.     }
    27.  
    28.     void Update() {
    29.         if(health <= 0) {
    30.             Destroy (gameObject);
    31.         }
    32.         if (startFight) {
    33.             animator.SetInteger ("bossAttack", -1);
    34.             startFight = false;
    35.             animator.SetInteger ("bossAttack", 0);
    36.         }
    37.         Debug.Log(animator.GetInteger("bossAttack"));
    38.     }
    39.  
    40.     void OnTriggerEnter2D(Collider2D col) {
    41.         if (col.tag == "Player") {
    42.             player.health -= damageToPlayer;
    43.         }
    44.         if (col.tag == "Bullet") {
    45.             health -= player.damage;
    46.         }
    47.     }
    48. }
    49.  
    50.  
    There's a lot going on, but my problem lies in the Update method. Despite startFight being true, the integer is never updated. It stays at 0 during the runtime. Commenting out startFight = false did not stop it from happening, the int just never updates to begin with. Can anyone explain why? Thanks!
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Because you are immediately setting the value back to 0.

    Code (csharp):
    1. if (startFight) {
    2.     animator.SetInteger ("bossAttack", -1);
    3.     startFight = false;
    4.     animator.SetInteger ("bossAttack", 0);
    5. }
     
    zombiegorilla likes this.
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,950
    Correct, it will always be 0. You are setting it to 0 on line 35.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    If you want the behaviour of a value that is set true then immediately reverts back to false, use a trigger.
     
  5. GamesOnAcid

    GamesOnAcid

    Joined:
    Jan 11, 2016
    Posts:
    280
    Excuse the late response, but I should have clarified what I tried. I've removed and changed multiple lines, and nothing has changed the value. I obviously would have a non-functioning slice of code if it was working properly, but even if I explicitly set the value like this without the Update function:
    Code (CSharp):
    1. void Awake() {
    2.     animator.SetInteger("bossAttack", anyNumber); //Where anyNumber is any integer
    3. }
    It still remains at 0. Could this require a project refresh or reinstallation?
     
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Often this is caused by not actually having the proper animator controller selected. The value won't update if you aren't looking specifically at the animator controller that's attached to the object being animated.
     
    zombiegorilla likes this.
  7. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,950
    This. Also check to make sure in the Animator that the parameter is spelled the same, and that it is an int and not a float or something. It won't throw errors if you set or get something that isn't there.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    And that the variable your are using really is non zero and hasn't been overridden by the inspector settings.
     
    zombiegorilla likes this.
  9. Arsinx

    Arsinx

    Joined:
    Apr 14, 2014
    Posts:
    55
    Funnily Enough I ran into the same problem when setting animator values in start. They Seemed to always be 0. I've worked around it using a small delay when setting them in start.
     
    Whatever560 and nyizhar like this.
  10. nyizhar

    nyizhar

    Joined:
    May 24, 2020
    Posts:
    1
    Exactly!
     
  11. Whatever560

    Whatever560

    Joined:
    Jan 5, 2016
    Posts:
    401
    When you programatically set the animator I believe you need to wait a frame to set the value.

    Code (CSharp):
    1.  
    2.                             modelAnimator.runtimeAnimatorController = previewController;
    3.                             StartCoroutine(SetIntegers());
    4.  
    5.                             IEnumerator SetIntegers()
    6.                             {
    7.                                 yield return null;
    8.                                 modelAnimator.SetInteger(nameof(EAttackType), (int)attackType);
    9.                                 modelAnimator.SetInteger(nameof(EMoveType), (int)moveType);
    10.  
    11.                             }