help for start my project...

I want to make an application in which some squares are generated at random positions and that they are destroyed when the user clicks on it (score increase) …

How can i realize that?
Actually I have a background with a script wich generate the square (prefabs)…
I can’t make the script that detect when a square is pressed… help me :frowning:

var enemy : GameObject;
var timeDelay = 5;
 
function Start() {
    while (true) {
        yield WaitForSeconds(timeDelay);
        Instantiate( enemy, Vector3(Random.Range(-4.5,4.5),Random.Range(-8.6,8.6),-0.1), Quaternion.identity );
    }
}

You’ll need something in your update function (something that runs once per frame) that acknowledges the player has clicked on a block, and increment the score by X points. You’d need to declare an int to store the users score.

Unityscript isn’t my strong point but your code should look something like this.

 function Update() {
    if (Input.GetMouseButtonDown(0)) {
        enemy.renderer.enabled = false;
        playerScore++;
    }
 }

Notice that I disable the renderer rather than use ‘destroy’. Once something is destroyed, it can’t be instantiated as it was originally.

GetMouseButtonDown

Attach a script with an OnMouseDown() method to the square objects, where you destroy it, like this:

using UnityEngine;

public class Obliterate : MonoBehaviour
{
	void OnMouseDown()
	{
		Destroy(gameObject, 0.01f);
	}
}