Pick Up Coins

Hi guys, i tried writing a script to pick up coins so that my score goes up and the coins dissapear but now when i try my main character (named gamer) runs again the coins and keeps running but nothing happens, score doesn’t go up or coins don’t dissapear, someone knows a script that i could use,

ty in advance.

Your code should work so let’s check what could be the problem.

  1. coin does not enter trigger
  2. trigger does nothing

So first look that your debug message does get displayed.

  print("OnTriggerEnter() was called");

If that did not get called check the following:

  1. Coin has a collider/character controller attached to it, and it’s enabled
  2. Player has a WORKING collider attached to it which is a TRIGGER
  3. check this with a box collider, see that IsTrigger is set to true (in the inspector) tip: always use simple shapes for testing scripts
  4. If you use a mesh collider (which is probably a bit of a waste for a pickup trigger) it might not work out of the box, so check if it works.
    Note that a collider can not be something to collide with AND a trigger at the same time(google raycast vs trigger).
    For a mesh to work as a collider it needs to fulfill certain requirements, it will not work out of the box if all it’s faces are not convex.
    Go to the models import setting, and click on Optimize Mesh(Not sure if it really does but I think that might fix some issues) and more importantly “convex”.
    What “convex” does is, as far as I unterstand, it pretends that the model’s faces are convex even if they aren’t. Collision might be slightly off, the resulting mesh collider might for example ignore that a bowl has an inside but at least it will work. And for a trigger that’s more than good enough.
  5. Check that you have attached your script to the player-object and that its activated. Check that it is running.

If your trigger get’s succesfully entered and still nothing happens the problem is probably somewhere between your score-variable and it’s output.

To make the coins disappear and check the score change:

if (other.tag == "coin") {
    Destroy(other);
    print(score);

}
  1. Check that you haven’t done something like declaring the coins a trigger. I suspect you’re using a character controller or such for the player and have no trigger attached to it. If this is the case add component-physics-box collider to the gamer-object, size it accordingly and click “istrigger”.