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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unable to play animation on instantiated prefab

Discussion in 'Scripting' started by cristo, Jul 23, 2019.

  1. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Hi,
    I have an animation clip on a prefab, specifically I'm animating the alpha channel on the GameObject to make it fade in when it is instantiated. The GameObject ("DustCloud ") instantiates fine but no animations can be called on it. My code is kind of a mangled mess of different attempts at this point. Any advice about an approach would be great. The animation has been marked legacy.
    Code (CSharp):
    1.    if (other.CompareTag("Player"))
    2.         {
    3.             Instantiate(DustCloud, PlayerCam.transform.position, PlayerCam.transform.rotation);
    4.             DustCloud.GetComponent<Animation>().Play("DustUp");
    5.             //DustCloud = (GameObject)Instantiate(gameCharacterPrefab, PlayerCam.transform.position,                            Quaternion.identity);
    6.             Debug.Log("DustCloud");
    7.             //anim = DustCloud.GetComponent<Animator>();
    8.             //anim.Play("DustUp");
    9.          }
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    Maybe it should be
    Code (CSharp):
    1. DustCloud.GetComponent<Animator>().Play("DustUp");
     
    vuribe_p likes this.
  3. cristo

    cristo

    Joined:
    Dec 31, 2013
    Posts:
    265
    Thanks for the reply. I'll give it a try.