Problem with Collecting Stars

I’ve three stars in my Scene and a starUI(i actually don’t knw whats right word for it), Any way … when the player collides with each star , Deactivate that star and upgrade starUI, show collected stars with animation. and I have a variable called “star Collected” which will be incremented when player collides with star … depending on that value of starCollected some animation will run… I’ve already added those animation not shown in the script below… the problem is with that starCollected varibale, some times it is incrementd 2times and sometimes 4 even when player collides with only one star , thats why my animation couldn’t run well…

if this is not the right way, can anyone please suggest me a better way??

the clear example of what I want is in Cut the rope Game, if u played it… :slight_smile:

private int starCollected = 0;

void OnTriggerEnter(Collider otherObject)
	{
		if (otherObject.gameObject.tag == "player") 
		{	
			
			starCollected++;
			gameObject.SetActive(false);
			ScoreCheck();


			//Debug.Log ("StarCount = " + starCount);
		}

	}




	void ScoreCheck()
	{
			
			switch(starCollected)
			{
			case 1:
				// run some animation
				break;

			
			case 2:
				// run some other animation
				break;

			
			case3:
				// run some animation
				break;
			}
					

		}

P.S. this Script is attaced with all stars…

You need to make the collision script only run once. Try this:

  private int starCollected = 0;
    private boolean doneOnce = false;
     
    void OnTriggerEnter(Collider otherObject)
        {
            if (otherObject.gameObject.tag == "player") 
            {   
    		if (!doneOnce){ //Test if the process has already been done before: if not, run the process
     
                		starCollected++;
                		gameObject.SetActive(false);
               		ScoreCheck();
     			
    			
                 		//Debug.Log ("StarCount = " + starCount);
    			doneOnce = true; //mark the process as complete.
    			}
            }
     
        }