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?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Melee : MonoBehaviour {
public Animation anim;
public vp_FXBullet damage;
public Transform Area;
public AudioClip soundClip;
public AudioSource audioSource;
// Use this for initialization
void Start () {
for (int i = 0; i < 10; i++)
{
Instantiate(damage, new Vector3(i * 2.0f, 0, 0), Quaternion.identity);
}
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.B))
{
anim.Play("Melee");
vp_FXBullet damageInstance;
damageInstance= Instantiate(damage, Area.position, Area.rotation) as vp_FXBullet;
}
if (Input.GetKeyDown(KeyCode.B))
{
audioSource.PlayOneShot(soundClip, 3.0f);
}
}
}
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?
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.
You can get the animation component from instantiated gameobject like you would with any object.
public GameObject prefab;
Animation anim;
public void Start()
{
GameObject obj = Instantiate(prefab) as GameObject;
anim = obj.GetComponent<Animation>();
}
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.