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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Controller Help

Discussion in 'Scripting' started by LasJJ, Aug 26, 2022.

  1. LasJJ

    LasJJ

    Joined:
    May 31, 2022
    Posts:
    10
    How do I trigger another game object's animation/animator?
    This is the reference for the other object that I want to trigger. The script below is attached to the main object, and should trigger the other object's animation. But I keep getting console log message (Animator is not playing an AnimatorController)

    public class TriggerScript : MonoBehaviour {
    public GameObject otherObject;
    Animator anim;

    private void Start()
    {
    anim = otherObject.GetComponent<Animator>();
    }

    private void OnMouseDown()
    {
    anim.SetTrigger("Active");
    }
    }

    "Active", is the name/string of the trigger from the animator of the other game object. On the inspector tab in unity, I drag and drop the other game object in the public variable. Please help
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Is the Animator active and enabled at the time of execution?

    More specifically, are you calling this on a prefab instead of a GameObject in the scene?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,962
    Based on the error message it's almost certainly this.

    if you have dragged the prefab into this slot you need to first Instantiate<T>() that prefab and keep a reference to the GameObject returned from Instantiate and use that for your
    otherObject


    In other news, I highly recommend using meaningful names, such as:

    Code (csharp):
    1. [Header( "Fill this in with the player prefab")]
    2. public GameObject PlayerPrefab;
    3.  
    4. // assign the Instantiate-d player to this and only use this for everything at runtime
    5. private GameObject InstantiatedPlayer;
    Always seek to reduce confusion. If you use the word Object you are asking for confusion.
     
  4. LasJJ

    LasJJ

    Joined:
    May 31, 2022
    Posts:
    10
    Thank you for the reply. Yes it is a prefab.
     
  5. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    You can't do operations like this on Prefabs, it needs to be instantiated first.
     
  6. LasJJ

    LasJJ

    Joined:
    May 31, 2022
    Posts:
    10
    Thankyou for the reply. Forgive my errors, I will take that on board.
     
  7. LasJJ

    LasJJ

    Joined:
    May 31, 2022
    Posts:
    10
    Instantiate first, then call the animation. Thankyou again.
     
  8. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Make sure to call the animation on the newly Instantiated object ;)