int variable seem to have multiple value at the same time

So I am making a 2D top down shooter similar to Asteroids. I want to add some stars as background and I want them to be generated dynamically in my code. Since there are no boundaries to where the player can go and I don’t want to have thousands of stars on my scene, I made so the stars destroy themselves when they are not visible by the camera. Now here is my problem: I created a variable to count the number of stars, the idea being that whenever I destroy one star, I generate another one immediately. Seems simple enough and yet…

So here are my two class: one is attached to the star itself and the other(StarHandler) is attach to an empty game object.

public class Star : MonoBehaviour
{
    public StarHandler starHandler;
    private bool hasBeenVisible = false;

private void OnBecameVisible()
{
    hasBeenVisible = true;
}

private void OnBecameInvisible()
{
    
    if (hasBeenVisible)
    {
        Destroy(gameObject);
        starHandler.RemoveStar();
    }

}

}

This is the the class attached to the sprite, it check if the star is still in the camera and if not, it destroys it and call a the removeStar function in StarHandler.

public class StarHandler : MonoBehaviour
{
    public GameObject player;
    public GameObject pfStar;
    private int nbOfStar = 0;

void Start()
{        
    Debug.Log(nbOfStar + " Debug 1");
    // This one shows 0 as intended

    AddStar(10);
}

void AddStar(int starNb)
{

    for (int i = 0; i <= starNb; i++)
    {
        Vector2 pos = new Vector2(Random.Range(-9, 9), Random.Range(-5, 5));

        GameObject star = Instantiate(pfStar, pos, Quaternion.identity);

        Debug.Log(nbOfStar + " Debug 2");
        // this one shows 0 as it is supposed to
    }

    nbOfStar += starNb;

    Debug.Log(nbOfStar + " Debug 3");
    // This one shows 10 as it is supposed to
}
public void RemoveStar()
{
    nbOfStar--;

    Debug.Log(nbOfStar + " Debug 4");
    // this one is weird, the value seems to decrease each time I try the game. I tried to reset the
    // component in the inspector but it changes nothing. The value is always negative and can range form -250 to 
    // -1050 and possibly more, it seems somewhat random to me.
          
}

void Update()
{
    Debug.Log(nbOfStar + " Debug 5");
    // This one is weird too. The nombre of star will increase up to 35 in console just as intended,
    //  and then nothing that I have tried as managed to change the number.
    // I can destroy every star in the game and still the console will show me 35...

    if(nbOfStar < 35)
    {
        Vector2 pos = new Vector2(Random.Range(player.transform.position.x + 9, player.transform.position.y - 10),
            Random.Range(player.transform.position.x + 18, player.transform.position.y + 10));
        GameObject star = Instantiate(pfStar, pos, Quaternion.identity);
        nbOfStar++;

    }

}

}

This is the second script, I have put comments bellow all the Debug.Log() so you can better understand the problem I face.

Hm I think I have the same problem… really weird because I wanted to assign ids for my leaderboards, but the value is at 0 and the ID at the same time which confuses me a LOOOOT. Hope someone knows what the issue is. Maybe int values are just buggy :confused:

A couple of things…

It’s not a good idea to keep on instantiating and destroying stars. There’s a background task that has to keep going round releasing memory that has been freed. This process is called Garbage Collection. Why not simply change the position of a star when it’s no longer visible?

There’s an alternate technique, called Object Pooling where you have a number of stars available and, when you need a new one, you take it from the pool of unused stars. Finished with a star? Send it back to the pool. You’ll find plenty of tutorials on Youtube about object pooling.

One more thought, though. Do you really need to create stars in the first place? For 2D games, there is a very common technique of having a scrolling background, which can be horizontal, vertical or both. Again, hunt out a tutorial. Create a background with stars already in place and you can just scroll it as required and, when you go off the edge, it gets repositioned.