Trying to set random buttons to right/wrong c# unity HELP!

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);


    }
       
}

Cool… Maybe you have a script with a method that takes an integer parameter.
Each button you add an OnClick event and point it to that method and then type in 1-9 as their parameter for the event/method.
Wherever you’re making the question/setup (with the right answer) assign the answer that can be verified when the method receives its function call… compare and print out your answer accordingly.
That make sense?

What have you tried so far? What’s not working?

Sorry forgot to link the script that’s what I have is it not? It also wouldn’t let me use an int parameter since it tries reading it as a bool in the if statement

Okay, so you didn’t have the script at first. Maybe I’m a little fuzzy on how this game works. Does it make 9 buttons and then one has the right answer? (just checking, not confused with it makes between 1 and 9 buttons?).

If you have Image references, and you drag those into the script in the inspector, you do not have to use GetComponent() etc to access them, again.

You can’t really check for mouse input the way you have it setup.
Your game will launch, do the randomCircles () … try to call the function ,and that’s the end of it.

If you re-read my post maybe you will see something to it?

Oh, you don’t need new Random().
And Random.Range is exclusive… not sure what the point of doing Random.Range (1, 1) is, as you’ll always get 1, afaik.

The game is laid out like a phone lock screen. Sort of like this

I was using Random.Range(1,1) because I was just test the first button. So if i click it it should say correct. Anyway sorry I am just getting into c# so I don’t know how to keep stuff simple.

So should this be my method?

    public void OnButtonClick(int buttonNumber){
        if (buttonNumber == buttonNumber) {

            Debug.Log ("Right");
        }
        else{
           
            Debug.Log ("Wrong");
        }
    }

Well, sort of… if you followed the part about assign integers 1 through 9 to the parameters of each Buttons OnClick method that calls a function (like the one you wrote)… that’s good
Of course you don’t want to compare buttonNumber == buttonNumber…
you’d need : buttonNumber == RandomlyChosenNumber or something like that, but yes that is the idea :slight_smile:

Ok so I think I have the idea now. I want it to equal the randomNumber I created. The thing is how do I call upon it in my OnButtonClick method if its in my RandomCircles method?

Well, you can’t (right now) :wink:
Of course, you want to move the variable for the random number you choose to be outside of the method to begin with.
Then, when you call the method, you assign it there (there meaning where you make the random selection*), but it’s also available to check against in your OnClick compare thing.

I am not sure I understand what you mean :frowning: When I moved it outside the method I got a warning telling me to put it inside my own or Start/Awake/Update methods

k because you didn’t understand what I meant, I want to check what we’re talking about. lol
How do you call upon it in your OnButtonClick if it’s in the RandomCircles method, you asked… To what were you referring ? :slight_smile:

So my OnButtonClick method is trying to access randomNumber which is in the RandomCircles Method. It shows up as an error saying “The name randomNumber does not ‘exist’ in this context”

public void OnButtonClick(int buttonNumber){
        if (buttonNumber == randomNumber) {

            Debug.Log ("Right");
        }
        else{
           
            Debug.Log ("Wrong");
        }
    }


    void RandomCircles(){
        int randomNumber = Random.Range(1,1);
        switch (randomNumber) {
        case 1:
            Debug.Log ("One");
            Left.GetComponent<Image> ().color = new Color32 (157, 240, 136, 255);
            break;

Okay, that’s good (at least we were/are talking about the same thing)
So, declare ‘randomNumber’ outside of the RandomCircles method. :slight_smile:

// not in a method
int randomNumber = 0;

// in the RandomCircles
randomNumber = Random.Range(lowPart, HighPart+1);

Whats with the HighPart+1?

Also it is just reading the randomNumber outside as a 0 and saying that it is wrong.

Sorry, highPart+1 for your test is irrelevant, … at the time I was thinking if you want 0-8 (or 1-9) you’d want :
Random.Range(0,9) or Random.Range(1,10) + 1; forget about that for now…

Saying it’s wrong? Can you paste what you did?

I think it’s just keeping the int as 0 for randomNumber when it’s on the outside like that.

    int randomNumber = 0;

    // Use this for initialization
    void Start () {
        RandomCircles ();

    }

    public void OnButtonClick(int buttonNumber){
        if (buttonNumber == randomNumber) {

            Debug.Log ("Right");
        }
        else{
           
            Debug.Log ("Wrong");
        }
    }


    void RandomCircles(){
        int randomNumber = Random.Range(1,1);
        switch (randomNumber) {
        case 1:
            Debug.Log ("One");
            Left.GetComponent<Image> ().color = new Color32 (157, 240, 136, 255);
            break;

Yes & no…
It is, because you re-declared it inside the method. Remove ‘int’ from the one inside the method :slight_smile:

Ok so it is sort of working. I have the script applied to all 9 buttons but it only says wrong when I click 4 of the 9. When I click the one that says right it works. So the problem now is just that some of the buttons aren’t coming out as wrong.

Okay, so you don’t want this script on all of the buttons. You want it on 1 object (maybe 1 button or maybe some other object, that doesn’t matter which, just that it’s 1).
Then, you want to be sure the OnClick function references that object, for the game object part of the OnClick event, and passes the parameter (1 … 9) as the argument.