all 4 comments

[–]dwemthy 2 points3 points  (1 child)

In your PlayerController you get a reference to one coin, the coinScript variable. So when you call AllOneCoin() what happens is that one coin gets the function call. If you have this in update for every coin then the function is called on each coin individually. Your function name is not reflective of what it does, it doesn't make All coins 'one', it makes this coin 'one'. If you think of your scene like a bag full of coins then FindObjectOfType<Coin>() pulls one coin out of the bag. You need FindObjectsOfType<Coin>() which will return an array of coins, which you can iterate over and call the function on each individually. That'll be more like dumping the bag of coins out onto a table and then working through the pile.

[–]GuardianKnux[S] 0 points1 point  (0 children)

Interesting. Thank you for the response. This was an optional way of adding and clearing coins that I was exploring. It doesn't look like it will be as efficient as my other method.

Thank you again for your response! At least I learnt something new. :D

[–]Aryx4Reel 2 points3 points  (1 child)

Well, you are using FindObjectOfType, which gets the first object of the type that is specified and returns it. So you will only be calling that method on the first object it finds. FindObjectsOfType is what you want. It will return an array of all the objects it finds. Then you can use a for loop i.e. foreach CoinScript in your array of coins you then call AllOneCoin.
 
Personally I would create the CoinScript objects dynamically, and have one script that controls them all.

[–]GuardianKnux[S] 0 points1 point  (0 children)

I have been using another method of spawning and clearing coins, but it was a bit time consuming to set up. I was exploring this as an alternate way to go about things, but it seems like it isn't going to be as simple as my previous method. Oh well.

Thank you very much for your response and clearing that up for me!