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
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.