trigger problem when achieving high velocity

Hi, I have a bike that is accelerating and collecting coins, so when i get a trigger with 3 near coins when the bike is on high velocity the count is mistaken (some times give 5 or 6)
do you guys know why ?

In the bike script:

void OnTriggerEnter(Collider collision)
	{
		if(collision.transform.tag == "Coin")
		{	
		  	gameManager.BroadcastMessage("IncCoinCount",SendMessageOptions.RequireReceiver);
			print("You got a coin !");
		}
	}

The Game manager code

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {
	
	public int coinsOwned;
	public int score;
	
	void OnGUI()
	{
		GUI.Box(new Rect(10,10,120,30),"Coin owned: " + coinsOwned.ToString());
	}

	
	void IncCoinCount()
	{
		coinsOwned++;
	}
}

Images (Please read what’s on the left top):

Before:


After:


Console:

I don’t see any code for removing coins on collision. It could be that you are colliding with a single coin multiple times before it is destroyed.

I don’t think that’s the case, this code is attached to the coin:

	void OnTriggerEnter(Collider collision)
	{		
		if(collision.transform.parent.tag == "Player")
		{
			Destroy(gameObject);
		}
	}

On a side note: if you have a reference to GameManager instance in bike collision then why you do the broadcast instead of simply:

gameManager.IncCoinCount();

EDIT:
put a breakpoint inside your if(collision.transform.tag == “Coin”), debug and see what is going on

EDIT:
Also, since the print(“You got a coin !”); is inside the collision condition then it must be that you are somehow getting more collisions then expected

EDIT:
I’m not experienced with Unity and don’t know its inner workings but it seams to me like at high velocities it’s having problems with detecting collisions so sometimes in one game loop one of the triggers (one that prints to console) gets triggered but the other (one that destroys coin ) doesn’t and so then they get triggered (or not) again on the same coin the next game loop. Putting your destroy code for coins in the same trigger where you call the IncCoinCount function should solve the problem.

void OnTriggerEnter(Collider collision)
{
    if(collision.transform.tag == "Coin")
    {   
        gameManager.IncCoinCount();
        Destroy(collision.gameObject);
        print("You got a coin !");
    }
}

For the gameManager.IncCoinCount();
gameManager is a game object and not a class, so, that won’t be possible.
and those debugging advices won’t work with unity :confused:
but thanks for the help anyways.

You commented on all points except the one that really matters - that solves your problem.

Yes gameManager is an object and this is why gameManager.IncCoinCount(); will work. For that method call to work if gameManager was a class the method would need to be static but it’s not.

What do you mean this debugging advices won’t work? Of course they will.
http://docs.unity3d.com/Documentation/Manual/Debugger.html

If it’s the classic problem when detecting collisions at high speed, you can try some of these solutions:

I think the classic problem is that collision won’t trigger. It this case it triggers too much. Solution has been proposed but OP seam to have missed it.

Hi, I didn’t understad what do you mean by reduce the physics time step.(i tried the other two solutions and they didn’t work)
Thank you for the help

Whenever i try this code

if(collision.transform.tag == "Coin")
		{
        gameManager.IncCoinCount();
        Destroy(collision.gameObject);
        print("You got a coin !");
   		}

I get an error.

1398008--72395--$Capture.PNG

Youshould have replied: “Oh no, gameManager variable is does not hold instance of GameManager class instead it’s an instance of GameObject class but I called it gameManager so that it is very very confusing”.

What’s the idea behind this? Can you show me code where you initialize gameManager variable?

public GameObject gameManager;

That’s definition. Initialization would be the part where you first assign it a value:

gameManager = ...

Given the definition I’m guessing that gameManager variable is being assign a GameObject type object relevant to your GameManager class. In other words GameManager class script is set as component of GameObject stored in gameManager variable. If so then you need to do:

(gameManager.GetComponent(typeof(GameManager)) as GameManager).IncCoinCount();

Now this is rather ad hoc but will do assuming that’s the only place you want to reference your GameManager class instance. Thing is that it’s not.
As the name suggests GameManager is suppose to be a back bone of your entire code architecture for the game. You will want to reference it multiple times in multiple places. Given that you typically will want only one instance of that class you should make it a singleton.

Now, you want your GameManager class to be a singleton anyway because of how Unity is designed. If you put your game object with GameManager attached into scene the isntance of GameManager will live only within the scene and it will be destroyed upon changing scenes (like when loading next level) and that’s not what you want. You want your GameManager class instance to be program scope and not scene scope and making it singleton is the solution.

After you make it a singleton you just do:

GameManager.GetInstance().IncCoinCount();

EDIT: What about the original problem? Did the suggested solution help?

EDIT: I’ve noticed you have a habit of making everything public. You don’t want to make anything public unless you really have to. Like in:

public class GameManager : MonoBehaviour {
   public int coinsOwned;
   public void IncCoinCount()
    {
       coinsOwned++;
    }

You don’t want your GameManager class user to see coinsOwned variable because he can then manipulate it beyond your control thus making it’s content unpredictable and unreliable and thus possibly breaking your class inner workings. It should be private or protected (private assuming we don’t typically want our GameManager class serve as base for a child). User still can manipulate it using IncCoinCount method but only to the extend you allow and under your control.
Encapsulation is one of the OOP golden rules and should always be followed.

http://www.tutorialspoint.com/csharp/csharp_encapsulation.htm

I really appreciate your help Pirs01, Thanks !
I hope the solution is there.

EDIT:
Even though you had all of these explanations for correcting the code, i still wonder why the trigger is acheived four times meanwhile i got 3 coins.