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 ?
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++;
}
}
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.
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
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.
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.
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?
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.
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.