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.
Your answer
 
             Follow this Question
Related Questions
Why is my camera constantly shaking? 0 Answers
Setting variable in prefab from a method called by prefab 1 Answer
FPS Microgame camera moving without input 0 Answers
Rect or Camera transform? 0 Answers