I am making an app in unity. It is a basic Simon game. I have buttons that get randomly selected 1-9. I want to be able to click on my correct button and have the console read out correct, and then click a wrong button and have the console read out wrong.
This is the first part I had. I thought this says when I click and buttonNumber is true write “Right” and if it isn’t true write “Wrong”
public void OnButtonClick(bool buttonNumber){
if (Input.GetMouseButtonDown(0) && buttonNumber) {
Debug.Log ("Right");
}
else{
Debug.Log ("Wrong");
}
}
This is the random number generator picking the first button for me. It sets it to true and should read back Right.
And every other button I have on screen should read back wrong if the script is applied to them.
void RandomCircles(){
Random RandomDot = new Random();
int randomNumber = Random.Range(1,1);
switch (randomNumber) {
case 1:
Debug.Log ("One");
Left.GetComponent<Image> ().color = new Color32 (157, 240, 136, 255);
OnButtonClick(buttonNumber: true);
break;
this is the full script if you need it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RandomCircle : MonoBehaviour {
public Image Left;
public Image Center;
public Image Right;
public Image LeftTwo;
public Image CenterTwo;
public Image RightTwo;
public Image LeftThree;
public Image CenterThree;
public Image RightThree;
private float buttonTimer = 1f;
// Use this for initialization
void Start () {
RandomCircles ();
}
public void OnButtonClick(bool buttonNumber){
if (Input.GetMouseButtonDown(0) && buttonNumber) {
Debug.Log ("Right");
}
else{
Debug.Log ("Wrong");
}
}
void RandomCircles(){
Random RandomDot = new Random();
int randomNumber = Random.Range(1,1);
switch (randomNumber) {
case 1:
Debug.Log ("One");
Left.GetComponent<Image> ().color = new Color32 (157, 240, 136, 255);
OnButtonClick(buttonNumber: true);
break;
case 2:
Debug.Log ("Two");
Center.GetComponent<Image> ().color = new Color32 (157, 240, 136, 255);
break;
case 3:
Debug.Log ("Three");
Right.GetComponent<Image>().color = new Color32(157,240,136,255);
break;
case 4:
Debug.Log ("Four");
LeftTwo.GetComponent<Image>().color = new Color32(157,240,136,255);
break;
case 5:
Debug.Log ("Five");
CenterTwo.GetComponent<Image>().color = new Color32(157,240,136,255);
break;
case 6:
Debug.Log ("Six");
RightTwo.GetComponent<Image>().color = new Color32(157,240,136,255);
break;
case 7:
Debug.Log ("Seven");
LeftThree.GetComponent<Image>().color = new Color32(157,240,136,255);
break;
case 8:
Debug.Log ("Eight");
CenterThree.GetComponent<Image>().color = new Color32(157,240,136,255);
break;
case 9:
Debug.Log ("Nine");
RightThree.GetComponent<Image>().color = new Color32(157,240,136,255);
break;
}
}
// Update is called once per frame
void Update () {
TimerCountdown ();
}
void TimerCountdown(){
buttonTimer -= Time.deltaTime;
if (buttonTimer < 0) {
TimerZero ();
}
}
void TimerZero(){
Left.GetComponent<Image>().color = new Color32 (38,38,38,255);
Center.GetComponent<Image>().color = new Color32 (38,38,38,255);
Right.GetComponent<Image>().color = new Color32 (38,38,38,255);
LeftTwo.GetComponent<Image>().color = new Color32 (38,38,38,255);
CenterTwo.GetComponent<Image>().color = new Color32 (38,38,38,255);
RightTwo.GetComponent<Image>().color = new Color32 (38,38,38,255);
LeftThree.GetComponent<Image>().color = new Color32 (38,38,38,255);
CenterThree.GetComponent<Image>().color = new Color32 (38,38,38,255);
RightThree.GetComponent<Image>().color = new Color32 (38,38,38,255);
}
}
