Prefab animation issues

Hello folks,

I’m currently experiencing some problems with a prefab, I instantiate at runtime.
This prefab already has an animation clip assigned. Now, I simply want to play back the animation.

Here is my code:

public var prefab : GameObject; //assigned in inspector

function Start() {
    Instantiate(prefab, Vector3(0,0,0), Quaternion.identity);
    prefab.animation.Play(); //default animation clip assigned in inspector
}

The animation doesn’t play at all.

Interesting fact is, that if I check “Play Automatically” in inspector, it plays back just fine.

Any help appreciated. Thanks in advance.

You’re not assigning the object to a variable when you instantiate it.

–Eric

Thanks, that makes sense. It works now.

public var prefab : GameObject; //assigned in inspector 
private var instantiatedPrefab : GameObject;

function Start() { 
  instantiatedPrefab =  Instantiate(prefab, Vector3(0,0,0), Quaternion.identity); 
   instantiatedPrefab.animation.Play(); //default animation clip assigned in inspector
}

but i still have a question.
my question like this:
my code:

public class hk: MonoBehaviour 
{
	float AnmTime = 0.0f;
	public GameObject HookPre;
	GameObject Hook;
	// Use this for initialization
	void Start () 
	{
		Hook = (GameObject)Instantiate(HookPre);
		Hook.animation["go"].speed=0.1f;
		Hook.animation["go"].time =0.1f;
		//Hook.AddComponent();
		Vector3 temp_pos =HookPre.transform.position;
		temp_pos.z+=1;
		Hook.transform.position = temp_pos;
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

my prefeab is a HookPre which contian an animation named “go”.
but when i edit it.cmd shows:

There is no ‘Animation’ attached to the “Hook(Clone)” game object, but a script is trying to access it.
You probably need to add a Animation to the game object “Hook(Clone)”. Or your script needs to check if the component is attached before using it.