Search Unity

SetTrigger animation works on some object, doesn't work in others.

Discussion in 'Animation' started by FireRuben, Aug 16, 2020.

  1. FireRuben

    FireRuben

    Joined:
    Nov 26, 2018
    Posts:
    18
    Hey all,

    I've been setting up interactions in our project recently, I have a player class that detects what the player can interact with.

    Here are two example scripts (that work without issue)

    Code (CSharp):
    1. public class DemoCabinetScript : MonoBehaviour {
    2.  
    3.     private bool cabinetOpen = false;
    4.     private GameObject doorOpenerLeft = null;
    5.     private GameObject doorOpenerRight = null;
    6.  
    7.     void Awake(){
    8.  
    9.         doorOpenerLeft = GameObject.Find ("DoorOpenerLeft");
    10.         doorOpenerRight = GameObject.Find ("DoorOpenerRight");
    11.  
    12.     }
    13.  
    14.     void Update(){
    15.  
    16.         /*
    17.         if(Input.GetKeyDown(KeyCode.C)){
    18.  
    19.             if(cabinetOpen){
    20.                 closeCabinet();
    21.             }else{
    22.                 openCabinet();
    23.             }
    24.  
    25.         }
    26.         */
    27.  
    28.     }
    29.  
    30.     public void openCabinet(){
    31.         doorOpenerLeft.GetComponent<FPEInteractableActivateScript>().interactionString = "Close cabinet";
    32.         doorOpenerRight.GetComponent<FPEInteractableActivateScript>().interactionString = "Close cabinet";
    33.         gameObject.GetComponent<Animator>().SetTrigger("OpenCabinet");
    34.     }
    35.  
    36.     public void closeCabinet(){
    37.         doorOpenerLeft.GetComponent<FPEInteractableActivateScript>().interactionString = "Open cabinet";
    38.         doorOpenerRight.GetComponent<FPEInteractableActivateScript>().interactionString = "Open cabinet";
    39.         gameObject.GetComponent<Animator>().SetTrigger("CloseCabinet");
    40.     }
    41.  
    42.     public void setCabinetOpen(){
    43.         cabinetOpen = true;
    44.     }
    45.  
    46.     public void setCabinetClosed(){
    47.         cabinetOpen = false;
    48.     }
    49.  
    50.     public bool isCabinetOpen(){
    51.         return cabinetOpen;
    52.     }
    53.  
    54. }
    55.  

    Code (CSharp):
    1. public class DemoCabinetInteractionScript : FPEInteractableActivateScript {
    2.  
    3.     public AudioClip cabinetOpen;
    4.     public AudioClip cabinetClose;
    5.     private GameObject cabinet;
    6.  
    7.     public override void Awake(){
    8.  
    9.         // Always call back to base class Awake function
    10.         base.Awake();
    11.  
    12.         // The cabinet can only be opened with two hands
    13.         canInteractWithWhileHoldingObject = false;
    14.  
    15.         cabinet = GameObject.Find("cabinet");
    16.         if(!cabinet){
    17.             Debug.LogError("DemoCabinetInteractionScript:: Cannot find cabinet Game Object!");
    18.         }
    19.  
    20.     }
    21.  
    22.     public override void activate(){
    23.  
    24.         if(cabinet.GetComponent<DemoCabinetScript>().isCabinetOpen()){
    25.             cabinet.GetComponent<DemoCabinetScript>().closeCabinet();
    26.             gameObject.GetComponent<AudioSource>().clip = cabinetClose;
    27.             gameObject.GetComponent<AudioSource>().Play();
    28.         }else{
    29.             cabinet.GetComponent<DemoCabinetScript>().openCabinet();
    30.             gameObject.GetComponent<AudioSource>().clip = cabinetOpen;
    31.             gameObject.GetComponent<AudioSource>().Play();
    32.         }
    33.  
    34.     }
    35.  
    36. }
    37.  
    Pretty self-explanatory, the cabinet has two animations, opening and closing, and an idle state.


    End result:


    This works without issue, the problem is, I tried replicating this using a chest and it stopped working for some reason.

    Here are the two scripts I'm using for the chest interaction:

    Code (CSharp):
    1. public class ChestAnimationInteractable : MonoBehaviour
    2. {
    3.     private bool chestOpen = false;
    4.     private GameObject ChestOpener = null;
    5.  
    6.  
    7.     void Awake()
    8.     {
    9.  
    10.         ChestOpener = GameObject.Find("ChestOpener");
    11.  
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         if(Input.GetKeyDown(KeyCode.C)){
    17.  
    18.             if(chestOpen){
    19.                 closeChest();
    20.             }else{
    21.                 openChest();
    22.             }
    23.         }
    24.  
    25.     }
    26.  
    27.     public void openChest()
    28.     {
    29.         ChestOpener.GetComponent<FPEInteractableActivateScript>().interactionString = "Close chest";
    30.         gameObject.GetComponent<Animator>().SetTrigger("ChestOpen");
    31.  
    32.     }
    33.  
    34.     public void closeChest()
    35.     {
    36.         ChestOpener.GetComponent<FPEInteractableActivateScript>().interactionString = "Open chest";
    37.         gameObject.GetComponent<Animator>().SetTrigger("ChestClose");
    38.     }
    39.  
    40.     public void setChestOpen()
    41.     {
    42.         chestOpen = true;
    43.     }
    44.  
    45.     public void setChestClosed()
    46.     {
    47.         chestOpen = false;
    48.     }
    49.  
    50.     public bool isChestOpen()
    51.     {
    52.         return chestOpen;
    53.     }
    54.  
    Code (CSharp):
    1. public class ChestInteraction : FPEInteractableActivateScript {
    2.  
    3.     public AudioClip chestOpen;
    4.     public AudioClip chestClose;
    5.     private GameObject hatch;
    6.  
    7.     public override void Awake()
    8.     {
    9.  
    10.         // Always call back to base class Awake function
    11.         base.Awake();
    12.  
    13.         // The cabinet can only be opened with two hands
    14.         canInteractWithWhileHoldingObject = false;
    15.  
    16.         hatch = GameObject.Find("Hatch");
    17.         if (!hatch)
    18.         {
    19.             Debug.LogError("ChestInteraction:: Cannot find hatch Game Object!");
    20.         }
    21.  
    22.     }
    23.  
    24.     public override void activate()
    25.     {
    26.  
    27.         if (hatch.GetComponent<ChestAnimationInteractable>().isChestOpen())
    28.         {
    29.             hatch.GetComponent<ChestAnimationInteractable>().closeChest();
    30.             //  gameObject.GetComponent<AudioSource>().clip = chestClose;
    31.             // gameObject.GetComponent<AudioSource>().Play();
    32.         }
    33.         else {
    34.             hatch.GetComponent<ChestAnimationInteractable>().openChest();
    35.             // gameObject.GetComponent<AudioSource>().clip = chestOpen;
    36.             // gameObject.GetComponent<AudioSource>().Play();
    37.         }
    38.  
    39.     }
    40.  
    41. }
    Everything is exactly the same except for the game objects being manipulated. When I test this out in-game though it doesn't work as intended.



    As you can see, it opens but doesn't close again. I think the code is functional and I messed up somewhere in the animator.

    Here's the cabinet animator for example:


    Here's the chest animator:


    See the difference? One gets stuck and the other doesn't, even though I double-checked all my settings and everything and all seems the same. So what's the deal? I also tried resetting the triggers, but I have the same outcome.

    What could be wrong here?
     
  2. FireRuben

    FireRuben

    Joined:
    Nov 26, 2018
    Posts:
    18
  3. FireRuben

    FireRuben

    Joined:
    Nov 26, 2018
    Posts:
    18
    bump x2
     
  4. FireRuben

    FireRuben

    Joined:
    Nov 26, 2018
    Posts:
    18
    bump x3