mercredi 4 septembre 2019

Use variable from one if statement in another if statement

I have this script on parrent object and I need to change textmesh with some random number. It is working fine but in second if statement rightNumber I need to set result, generated number * 3 and than use it in third if and change text to that number. It is working now that way that it sets all numbers, it also prints correct result from second if, but resultnumber is zero number, after next click it is changed to previous correct number. Resultnumber is also printed before rightnumber if statement. So how can I solve this. I need set wrongNumbers on some child objects, it is working, than rightnumber multiple by some number and then set resultNumber to result from second if.

static int result;

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Debug.Log("first);
        int rndNumber = Random.Range(1, 100);

        if (tag == "wrongNumber")
        {
            Debug.Log("wrongnumber ");
            TextMesh wrongnumber = GetComponentInChildren<TextMesh>();
            wrongnumber.text = rndNumber.ToString();
        }

        if (tag == "rightNumber")
        {
            TextMesh rightnumber = GetComponentInChildren<TextMesh>();
            rightnumber.text = rndNumber.ToString();

            result = rndNumber * 3;
            Debug.Log(result);
        }

        if (tag == "resultNumber")
        {
            Debug.Log("resultnumber ");
            TextMesh resultnumber = GetComponentInChildren<TextMesh>();
            resultnumber.text = result.ToString();
        }
    }

Aucun commentaire:

Enregistrer un commentaire