Increasing the score and destroying the object

So I am making this simple game where the player has to click on all the squares until there are no squares left on the screen. The score board should tally how many squares have been collected. When all the squares have been collected, an end menu appears.
This is the score script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Score : MonoBehaviour
{
    public Text scoreText;
    public int score;
   
    

    void Start()
    {
        //score = GetComponent<Text>();
    }

    void Update()
    {
        scoreText.text = "Score: " + score;
        if(Input.GetMouseButtonDown(0))
        {
            score++;
        }
        if (score >= 6)
        {
            SceneManager.LoadScene("end menu");
        }
    }
}

And this is the destroy square script:`

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Collect : MonoBehaviour {

  GameObject Square;
    

        
    void OnMouseDown()
    {
        Destroy(gameObject);
        
        
    }
   

}

`
As you can see the score increases just with a mouse click, i only want the score to increase when i collect (and destroy) the square. How can I do this?

Good day.

Its simple, if you make the square scritp to increase the point right before destroy it.

Just need that all squares acess the score variable of tje score script.

Bye.

You need the square script to have a reference to the score script, so it can tell it when to increase the score.

Square script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Collect : MonoBehaviour {

	void OnMouseDown() {
		FindObjectOfType<Score>().IncreaseScore(); // Find the Score script and tell it to increase the score
		Destroy(gameObject);
	}
}

Score script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Score : MonoBehaviour {

	public Text scoreText;
	public int score;

	void Start() {
		scoreText.text = "Score: " + score; // To make sure the score text is accurate
	}

	public void IncreaseScore() {
		score++;
		scoreText.text = "Score: " + score;
		if(score >= 6) { // We only need to check if the score is high enough when it increases, not every frame.
			SceneManager.LoadScene("end menu");
		}
	}
}

I also cleaned up your code a little. :slight_smile: hope this helps!