Hello Guest

Author Topic: Start Animation from Script  (Read 6636 times)

TTkJonas

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Start Animation from Script
« on: June 11, 2012, 01:31:19 pm »
Hi Unikron,

I have a animated Sprite in my Scene and unchecked the Play automatically flag. Now I added a trigger collider with the 2d toolkit and a simple script file to the animated Sprite Object.
 Now when the Player enters the trigger the animation shoult play. But ist dont works with my Script.

I get the Error
BCE0020: An instance of type 'tk2dAnimatedSprite' is required to access non static member 'Play'.

Sorry for this noob question^^, but I couldnt find a solution for this simple Script in the documentation without Buttons or other complex things. ;-)

Code: [Select]
function OnTriggerEnter(other : Collider)
{
if(other.transform.tag=="Player")
{
tk2dAnimatedSprite.Play("WasserSplash");
}
}


unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Start Animation from Script
« Reply #1 on: June 11, 2012, 02:16:59 pm »
Hi,

This is fundemental to how Unity works. I suggest looking into some Unity tutorials on how to bind components to your script. You can also do it like this, where you attach this script to the object with the animated sprite.

Code: [Select]
var animSprite : tk2dAnimatedSprite;
animSprite = GetComponent(tk2dAnimatedSprite);

function OnTriggerEnter(other : Collider)
{
if(other.transform.tag=="Player")
{
animSprite.Play("WasserSplash");
}
}

I'm guessing you've run "Set up for JS" as the error you're seeing implies that, but if you haven't you should run it first.


TTkJonas

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Start Animation from Script
« Reply #2 on: June 11, 2012, 02:43:36 pm »
Sorry for this stupid question. Normaly i know how to use getComponent, but I thought the tk2d functions are predefined like the iTween Plugin "iTween.Move()" etc ;-)

But know it works perfectly. Thank you very much! :D