In C#, how do I say if any 3 of the 10 objects in my scene are clicked, do something. I know how to say if 3 specific of the ten are clicked but it’s the “any” that baffles me. All help/advice is greatly appreciated.
You would need to keep track of the object that are getting clicked and have a counter that will increment by 1 each time something is clicked. Once that counter reaches (in your case 3) a number do something.
Okay here’s what I’ve done to keep score. I have a game controller called “GameControllerMain2” and a script I attach to all the objects that the game controller keeps score of called “EngnYouLoose”. this is the GameControllerMain2 script :
using UnityEngine;
using System.Collections;
public class GameControllerMain2 : MonoBehaviour {
private int score;
public GUIText ScoreText;
// Use this for initialization
void Start () {
score = 0;
UpdateScore ();
}
public void AddScore (int newscoreValue)
{
score += newscoreValue;
UpdateScore ();
if (score >= 3)
StartCoroutine(LoadLevel("Engine puzzle level", 3.0f));
}
IEnumerator LoadLevel(string level, float waitTime)
{
yield return new WaitForSeconds(waitTime);
Application.LoadLevel(level);
}
void UpdateScore ()
{
ScoreText.text = "Danger Level : " + score;
}
}
And this is the EngnYouLoose script:
using System.Collections;
public class EngnYouLoose : MonoBehaviour {
public int scoreValue;
public GameControllerMain2 gameControllerMain2;
void start (){
GameObject gameControllerObject = GameObject.FindWithTag ("GameControllerMain2");
if (gameControllerObject != null)
{
gameControllerMain2 = gameControllerObject.GetComponent <GameControllerMain2>();
}
if (gameControllerMain2 == null)
{
Debug.Log ("Cannot find 'GameControllerMain2' script");
}
}
// Update is called once per frame
void OnMouseDown() {
gameControllerMain2.AddScore (scoreValue);
}
}
Basically I want the score to update OnMouseDown (when the objects with EngnYouLoose script attached are clicked) and the same level to reload if the score reaches 3 but it doesn’t update the score for some reason. Any idea why? I don’t know if this is the easiest way to do it but I’ve got these same scripts working on another level with another game controller ( I know your should only have 1 game controller preferably)
Never mind I got it to work, I just had to change the public value from 0 to 1 in the inspector