Hello Guest

Author Topic: Play Animation of Instantiated Game Object  (Read 3113 times)

Kenzo

  • Newbie
  • *
  • Posts: 10
    • View Profile
Play Animation of Instantiated Game Object
« on: March 15, 2014, 08:06:04 am »
Hi i am trying to play animation of an instantiated (clone) object in an another script. But cant get it work.

So i am basically spawning an enemy (enemy has its own Tk 2d sprite and 2k 2d sprite animator component)

Here is a sample code :
Code: [Select]
tk2dSpriteAnimator anim;

 void Start()
    {
        anim = GetComponent<tk2dSpriteAnimator>();

        SpawnMonster();
    }

    void SpawnMonster()
{
  ...
  ....
  GameObject enemy = (GameObject)Instantiate(objectsToSpawn[i], spawnLocations[i].position, Quaternion.identity);
//how can i play animation of an instantiated enemy object here ?

         if (!anim.IsPlaying("Move Enemy"))  //this wont work because it only looks for animation component of current gameobject(which has no animation) i should reach the instantiated game object's animation but how ??
                        anim.Play("Move Enemy");
}
« Last Edit: March 15, 2014, 08:11:00 am by Kenzo »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Play Animation of Instantiated Game Object
« Reply #1 on: March 15, 2014, 11:18:18 am »
You need to get the animator on the enemy you've spawned...
tk2dSpriteAnimator thatAnim = enemy.GetComponetn<tk2dSpriteAnimator>();