Arithmetic Error output when using non-static global variable values (Unity C#)

I recently began implementing the concept of Singleton in my game.
My score keeping is however not giving the result I expect and I wonder whats wrong.
Now I try to increment the ‘score’ variable yet the arithmetic output is inconsistent in its increment.
Say when I collide with one coin object, I get and increment of 2 or more instead of 1.
Is there something I am doing wrong? I will be very grateful for your kind assistance.

Here is my code.
//------------------- MY GAME MANAGER SCRIPT
public class GameManager : MonoBehaviour
{
public static GameManager ManagerInstance { get; private set; }
public int score = 0;

private void Awake()
{
if (ManagerInstance == null)
{
ManagerInstance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}

}
// ----------- MY SCENE MANAGER SCRIPT
public class Scene_Manager : MonoBehaviour
{
public Text txtScore;
void Update()
{
txtScore.text = GameManager.ManagerInstance.score.ToString();

}
}
//----------- MY COIN SCRIPT
public class _enemy : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == “Player”)
{
GameManager.ManagerInstance.health --;
// Destroy(gameObject);
}
}
}

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

You may have more than one collider / trigger that is hitting. Print out what you hit with Debug.Log() to learn more.

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

This is my go-to for singleton implementation:

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

https://pastebin.com/SuvBWCpJ

Unity3D Singleton with Prefab used for predefined data:

https://pastebin.com/cv1vtS6G

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

public void DestroyThyself()
{
   Destroy(gameObject);
   Instance = null;    // because destroy doesn't happen until end of frame
}
1 Like

Wow. Thanx Kurt.:slight_smile: My issue is resolved. Very true. I was using two colliders. I have discarded one and it now works like a charm. I wanted to use circle collider for the character’s lower part and a box collider for the upper part (just my style) But I have deleted the circle collider attached a physic material with 0 friction to prevent the character from sticking to edges of the platform and its working perfectly.
Thanks so much for the help. I will take a look at your recommendations on the Singleton. Cheers :slight_smile:

1 Like