Hello Guest

Author Topic: Changing clip used in animated sprite in a script  (Read 5648 times)

mscowler

  • Newbie
  • *
  • Posts: 5
    • View Profile
Changing clip used in animated sprite in a script
« on: July 13, 2013, 04:25:35 pm »
I'm having trouble getting a script to change the clip used by an animated sprite. Here is my script.

#pragma strict

var myIndex:int;
var suit:GameObject[] = new GameObject[11];

function Start () {

suit[0] = transform.Find("CardSuit1").gameObject;
suit[1] = transform.Find("CardSuit2").gameObject;
suit[2] = transform.Find("CardSuit3").gameObject;
suit[3] = transform.Find("CardSuit4").gameObject;
suit[4] = transform.Find("CardSuit5").gameObject;
suit[5] = transform.Find("CardSuit6").gameObject;
suit[6] = transform.Find("CardSuit7").gameObject;
suit[7] = transform.Find("CardSuit8").gameObject;
suit[8] = transform.Find("CardSuit9").gameObject;
suit[9] = transform.Find("CardSuit10").gameObject;
suit[10] = transform.Find("CardSuit11").gameObject;

}

function Update () {

var i:int;
var j:int;
var k:int;

var sprite: tk2dSprite;
var animSprite : tk2dAnimatedSprite;

if (Global.cardValue[myIndex]==0) {

// Get next card in deck
Global.cardValue[myIndex] = Global.deckValue[Global.currentCardInDeck];
Global.cardSuit[myIndex] = Global.deckSuit[Global.currentCardInDeck];
Global.currentCardInDeck++;
i = Global.cardValue[myIndex];
k = Global.cardSuit[myIndex];

// Display card face with correct number
sprite = GetComponent(tk2dSprite);
if (i==1) sprite.SetSprite("card1");
if (i==2) sprite.SetSprite("card2");
if (i==3) sprite.SetSprite("card3");
if (i==4) sprite.SetSprite("card4");
if (i==5) sprite.SetSprite("card5");
if (i==6) sprite.SetSprite("card6");
if (i==7) sprite.SetSprite("card7");
if (i==8) sprite.SetSprite("card8");
if (i==9) sprite.SetSprite("card9");
if (i==10) sprite.SetSprite("cardM");

// Display animated suit characters
for (j=0; j<=10; j++) {
animSprite = suit[j].GetComponent(tk2dAnimatedSprite);
if(k==0) animSprite.Play("hatAnim");
if(k==1) animSprite.Play("frogAnim");
if(k==2) animSprite.Play("owlAnim");
if(k==3) animSprite.Play("dragonAnim");
}


The script is attached to a sprite called Card1 that has 11 children, CardSuit1-CardSuit11, that are sprites with animation. The animation attached to these sprites has 4 clips (hatAnim etc).

the code headed "// Display card face with correct number" works fine.

"suit" contains a gameObject list of the animated sprites (see the start function).

I know "suit" has been set up OK as I can activate/deactivate the animations using e.g. suit[j].SetActive(true);

The script compiles OK, but when I play the game I get the message "NullReferenceException:Object reference not set to an instance of an object" on whichever of the lines with "animSprite.Play" is executed.

Can anyone tell me what is wrong here?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing clip used in animated sprite in a script
« Reply #1 on: July 13, 2013, 04:30:52 pm »
Do those sprites actually have an animated sprite component on them? What can you see in the inspector?

mscowler

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Changing clip used in animated sprite in a script
« Reply #2 on: July 13, 2013, 05:39:37 pm »
Yes they do. In fact if I comment out the problematic code and play the game, the sprites animate fine using the original clip, set in the inspector ("hatAnim").
If I change the default in the inspector to any of the other three clips, this also works.
Also, as I stated before, I can turn the sprites on and off in the script using "SetActive."
The sprites were created using "Create Other>tk2d>Sprite With Animator".

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing clip used in animated sprite in a script
« Reply #3 on: July 13, 2013, 08:00:58 pm »
Ahaa. Now this makes sense.
I'm guessing you missed the migration 2D Toolkit 2.0 guide -
tk2dAnimatedSprite is now tk2dSpriteAnimator
http://unikronsoftware.com/2dtoolkit/doc/2.00/migration_guide.html

Apart from the class change, it should just work.

mscowler

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Changing clip used in animated sprite in a script
« Reply #4 on: July 13, 2013, 08:19:02 pm »
That fixed it! Thanks for the help.