0
\$\begingroup\$

I have a 2D game I'm making with Unity, and I want the player to be able to collect coins that I place all over the game. I used the coin prefab with a 2D circle collider and an animation to make it spin. The problem is that only the first coin I put into the scene shows up in the game, and when I collect that one coin, the counter shows that all the others have been collected as well. Here's the script I attached to the player for the coins:

private uint tokens = 0;

void OnGUI(){
    GUILayout.Label ("Tokens= " + tokens);
}

void CollectToken(Collider2D tokenCollider){
    tokens++;
    Destroy (tokenCollider.gameObject);
}

void OnTriggerEnter2D(Collider2D col){
    if (col.gameObject.CompareTag ("token")) {
        CollectToken(col);  
    }
}

This is my current setup for the object properties:

enter image description here

Thank you for any help!

\$\endgroup\$
4
  • \$\begingroup\$ Did you check that you're not moving the position of every coin to one position? \$\endgroup\$
    – Elva
    Jan 15, 2015 at 16:53
  • \$\begingroup\$ I have them in different positions in the scene, and when the game loads it looks like they either disappear or they all move to the position of the first coin \$\endgroup\$
    – Jordan
    Jan 15, 2015 at 16:56
  • \$\begingroup\$ Yes, did you check if the last thing isn't exactly what happens, and if it does debug why that is happening? Cause it's not in any of the code presented here. \$\endgroup\$
    – Elva
    Jan 15, 2015 at 17:06
  • \$\begingroup\$ It turns out it's the latter, all the prefabs are moving to one position. I've added a screen shot of the inspector, the prefab's position is set at 0, but it changes to this when I play \$\endgroup\$
    – Jordan
    Jan 15, 2015 at 17:18

1 Answer 1

0
\$\begingroup\$

I would add a bool for isCollected; so if(isCollected) token++

you want the coin to be inside a empty gameobject so when you go to animate it you are animating the child so it doesn't move all the coins to the position of where you created the first.

\$\endgroup\$
4
  • \$\begingroup\$ I tried that, but the coins are still moving to 0.25, -1.44 \$\endgroup\$
    – Jordan
    Jan 15, 2015 at 17:49
  • \$\begingroup\$ make sure to animate the child and not the position of the parent and your coin will animate correctly \$\endgroup\$ Jan 15, 2015 at 17:50
  • \$\begingroup\$ example would be Parent named Coin and child named ItemCoin if you animate the ItemCoin your good. just make sure the Animation does not show Coin : Position \$\endgroup\$ Jan 15, 2015 at 17:57
  • \$\begingroup\$ glad to hear :) \$\endgroup\$ Jan 15, 2015 at 18:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .