Search Unity

Animator trigger doesn't get set from script

Discussion in 'Scripting' started by Multiainen, Oct 20, 2019.

  1. Multiainen

    Multiainen

    Joined:
    Oct 9, 2019
    Posts:
    16
    So I've been trying to set up all the fundamentals of a first-person game, and managed to get completely stuck in the process (again). I have a basic script for picking up an object that I wanted to slowly start expanding on. Everything else (in its current state) works, but it doesn't trigger the animation for picking up the object. Here's the code, the relevant bits and the animator window:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pickup1 : MonoBehaviour
    6. {
    7.     Vector3 objectPos;
    8.     float timeBuffer = 2;
    9.     Animator animator;
    10.  
    11.     public bool canHold = true;
    12.     public GameObject item;
    13.     public GameObject tempParent;
    14.     public bool isHolding = false;
    15.  
    16.      void Start()
    17.     {
    18.         animator = GetComponent<Animator>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (isHolding==true)
    24.         {
    25.             item.GetComponent<Rigidbody>().velocity = Vector3.zero;
    26.             item.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
    27.             item.transform.SetParent(tempParent.transform);
    28.             item.transform.position = tempParent.transform.TransformPoint(0, 0, 0);
    29.         }
    30.         else
    31.         {
    32.             objectPos = item.transform.position;
    33.             item.transform.SetParent(null);
    34.             item.GetComponent<Rigidbody>().useGravity = true;
    35.             item.transform.position = objectPos;
    36.         }
    37.         if (timeBuffer >= 0)
    38.         {
    39.             timeBuffer -= Time.deltaTime;
    40.         }
    41.         if (Input.GetKeyUp("e"))
    42.         {
    43.             if (isHolding == true)
    44.             {
    45.                 if (timeBuffer <= 1)
    46.                 {
    47.                     isHolding = false;
    48.                 }
    49.             }
    50.         }
    51.     }
    52.  
    53.     void OnMouseOver()
    54.     {
    55.         if (Input.GetKeyUp("e"))
    56.             {
    57.             if (isHolding == false)
    58.             {
    59.                 isHolding = true;
    60.                 timeBuffer = 2;
    61.                 item.GetComponent<Rigidbody>().useGravity = false;
    62.                 item.GetComponent<Rigidbody>().detectCollisions = true;
    63.                 animator.SetTrigger("PL");
    64.             }
    65.         }
    66.     }
    67. }
    68.  
        Animator animator;
    animator = GetComponent<Animator>();
    animator.SetTrigger("PL");


    upload_2019-10-20_11-44-30.png

    Last I had an issue with a script, it was somehow resolved just by copying and renaming it. From what I found of this problem through googling, pretty much the same thing seemed to be what solved it for others. No such luck though: I've tried remaking the controller, renaming the trigger, using an int or bool instead, triggering it at different points in the script, making a separate script just to trigger it (for testing), double-checking the trigger's name for spaces... nothing works. If I manually set the trigger to active in the animator before running the game, it does work, and, as I said, the rest of the script works fine, so it has to be the script refusing to access the trigger. And I just can't figure out why.

    Thanks in advance for any help!