Toss Game (Selection_Script)

 using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;


public class Selection_Script : MonoBehaviour

{

    public Text selected_number_text;

    public Button button;

    public Button trybutton;

    public InputField input;

    public GameObject gameObj;


    int random =0;


    int[] selected_rand;

    int index = 0;


    bool repeated_number;


    int number_of_players;


    // Start is called before the first frame update

    void Start()

    {

        selected_number_text.text = "00";

        repeated_number = false;

        button.gameObject.SetActive(true);

        trybutton.gameObject.SetActive(false);


        input.gameObject.SetActive(true);

        gameObj.SetActive(false);

    }


    private void Update()

    {

        if(selected_rand != null)

        {

            if (index == selected_rand.Length)

            {

                button.gameObject.SetActive(false);

                trybutton.gameObject.SetActive(true);

            }

        }

    }


    public void RandomNumber()

    {

        int range = number_of_players + 1;

        random = Random.Range(1, range);

        ChooseNumber(random);

    }


    void SelectNumber(int rand)

    {

        selected_rand[index] = rand;

        index++;


        if (rand < 10)

        {

            selected_number_text.text = "0" + rand.ToString();

        }

        else

        {

            selected_number_text.text = rand.ToString();

        }

    }


    void ChooseNumber(int rand)

    {

        repeated_number = false;


        for (int i = 0; i< selected_rand.Length && repeated_number == false;i++)

        {

            if(selected_rand[i] == rand)

            {

                repeated_number = true;

                break;

            }

        }


        if (repeated_number)

        {

            RandomNumber();

        }

        else

        {

            SelectNumber(rand);

        }

    }


    public void SelectNumberOfPlayers(string s)

    {

        number_of_players = int.Parse(s);

        selected_rand = new int[number_of_players];

        input.gameObject.SetActive(false);

        gameObj.SetActive(true);

    }

}


0 Comments: