Hello Guest

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - mscowler

Pages: [1]
1
Support / Detecting Sprite touches
« on: July 20, 2013, 08:07:17 pm »
After looking at posts on this issue, I have inserted the following code in a script attached to all sprites that I want to check for being touched.

var hit:RaycastHit;
var ray:Ray;

if (Input.GetMouseButtonDown (0)) {
   ray = Camera.main.ScreenPointToRay(Input.mousePosition);
   if (collider.Raycast (ray, hit, 100.0)) Debug.Log (myIndex);
}

Code compiles OK, but when "ray = ..." line is executed, I get message "NullReferenceException UnityEngine.Camera.ScreenPointToRay(Vector 3 position)"

I have one camera (MainCamera). I have attached a box collider to the sprites.

Can anyone explain what I'm doing wrong.  I'm new to Unity and Java, so sorry if this seems trivial.

Also, any comments on whether or not the code as a whole will achieve my object (to obtain the index of the sprite that was touched) would be appreciated.

2
Support / 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?

Pages: [1]