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

Play specific animation on an instantiated prefab

Discussion in 'Scripting' started by MHNielsen, Jun 7, 2017.

  1. MHNielsen

    MHNielsen

    Joined:
    Jul 7, 2015
    Posts:
    33
    Hello,

    I have looked in the forum, but couldn't get an answer that was suited for my needs. But I want to play an animation on an instantiated prefab at runtime. So at runtime, I want to be able to press a button and the animation plays on the instantiated prefab. The code works when I manually assign the instantiated prefab when I'm running the scene, but I don't want to do it manually. But I cannot wrap my head around the solution.
    Here is my code. Any help to this on how I should assign the prefab in the public Animation variable?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Melee : MonoBehaviour {
    6.  
    7.     public Animation anim;
    8.     public vp_FXBullet damage;
    9.     public Transform Area;
    10.     public AudioClip soundClip;
    11.     public AudioSource audioSource;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.  
    16.         for (int i = 0; i < 10; i++)
    17.         {
    18.             Instantiate(damage, new Vector3(i * 2.0f, 0, 0), Quaternion.identity);
    19.         }
    20.      
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update () {
    25.  
    26.         if (Input.GetKeyDown(KeyCode.B))
    27.         {
    28.             anim.Play("Melee");
    29.  
    30.             vp_FXBullet damageInstance;
    31.  
    32.             damageInstance= Instantiate(damage, Area.position, Area.rotation) as vp_FXBullet;
    33.  
    34.         }
    35.  
    36.         if (Input.GetKeyDown(KeyCode.B))
    37.         {
    38.             audioSource.PlayOneShot(soundClip, 3.0f);
    39.         }
    40.      
    41.     }
    42. }
     
  2. Phorsaken

    Phorsaken

    Joined:
    Dec 12, 2013
    Posts:
    27
    Ok not clear on what you are exactly trying to do. The Start function you instantiate 10 objects it looks like. Are these the objects you are trying to get to play an animation or are you trying to set anim without have to manually assign it in the editor so it just works on any prefab you attach it to?

    -Alexander
     
  3. MHNielsen

    MHNielsen

    Joined:
    Jul 7, 2015
    Posts:
    33
    I am trying to make a melee system. When I launch the scene, a weapon prefab is being instantiated in front of the camera. But I want to assign the instantiated prefab to the anim variable. Not the prefab in the assets folder, but the instantiated version.

    Here you can see that I drag the prefab from the assets folder into the anim slot under my script. And when I run the scene, only the sounds are playing when I press on my keyboard. But when I assign the instantiated prefab, the animation are playing as well. So I want to grab the instantiated prefab from the start and assign it to the anim variable and not manually like in the video.
     
  4. Vipsu

    Vipsu

    Joined:
    Oct 8, 2012
    Posts:
    88
    You can get the animation component from instantiated gameobject like you would with any object.

    Code (CSharp):
    1.  public GameObject prefab;
    2.     Animation anim;
    3.  
    4.     public void Start()
    5.     {
    6.         GameObject obj = Instantiate(prefab) as GameObject;
    7.         anim = obj.GetComponent<Animation>();
    8.     }
    However I would really recommend you to learn how to use Animation Controllers, animation events and maybe even animation state behaviors because they're a lot better for literally everything you're trying to accomplish here.

    You could make idle, fire and melee states for each weapon and notify the script through animation events to spawn projectiles or activate/deactivate melee hitboxes specified by the weapon or just simply tell the script to check during specific part of the melee animation with something like Physics.OverlapSphere if it hits something.
     
    ChloeHanh likes this.
  5. MHNielsen

    MHNielsen

    Joined:
    Jul 7, 2015
    Posts:
    33
    Hello,
    Thank for the answer. I got it to work. I did this instead. Works now as it should.

    Code (CSharp):
    1. public class Melee : MonoBehaviour {
    2.  
    3.     public GameObject weapon;
    4.  
    5.     void Start () {
    6.  
    7.         weapon = GameObject.Find("TheInstantiatedObject");
    8.         anim = weapon.gameObject.GetComponent<Animation>();