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

Controlling an Animator Toggle Parameter

Discussion in 'Scripting' started by Studio_Akiba, Sep 20, 2017.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I followed a tutorial I found (a couple actually) and came up with a simple way to control a door animator which has been hooked up to control keyframe animations embedded into the model.

    This works fine when I manually change the Toggle Parameters Open/Close in the Animator window, but when attempting to do the same programatically (by triggering ToggleTrigger, with or without arguments) nothing changes.

    I have attached the script below the image of my Animator graph.

    Animator.png

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BulkheadDoorControllerScript : MonoBehaviour
    6. {
    7.     public Animator animator;
    8.  
    9.     //public variable
    10.     public bool isOpen;
    11.  
    12.     //set the animator based on input value currentPosition
    13.     public void ToggleTrigger(bool currentPosition)
    14.     {
    15.         if (currentPosition)
    16.         {
    17.             animator.SetTrigger("Open");
    18.         }
    19.         else
    20.         {
    21.             animator.SetTrigger("Close");
    22.         }
    23.     }
    24.  
    25.     //override method sets the animator, then toggles the public bool
    26.     public void ToggleTrigger()
    27.     {
    28.         isOpen = !isOpen;
    29.         ToggleTrigger(isOpen);
    30.     }
    31.  
    32. }
    I drag the Animator component onto the animator variable slot in the Inspector, then when triggering the change, seemingly nothing happens, in the Game, Scene or Animator windows.

    Can anyone see what exactly it is that I am doing wrong?
     
  2. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
    I have the same issue some time ago,
    try to change to the bool system: same thing, but just a bool

    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.  
    5.     public class BulkheadDoorControllerScript : MonoBehaviour
    6.     {
    7.         public Animator animator;
    8.  
    9.         //public variable
    10.         public bool isOpen;
    11.  
    12.         //set the animator based on input value currentPosition
    13.         public void ToggleTrigger(bool currentPosition)
    14.         {
    15.             isOpen = currentPosition;
    16.             animator.SetBool("Open", currentPosition);
    17.         }
    18.  
    19.         public void ToggleTrigger()
    20.         {
    21.             isOpen = !isOpen;
    22.             animator.SetBool("Open", isOpen);
    23.         }
    24.  
    25.     }
    26.