Unityで乱数を学ぶ

Unityで乱数を使ってみる。

 

今回はこんなスクリプトを用意してみた。

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class answer : MonoBehaviour
  6. {
  7.     private int min;
  8.     private int max;
  9.     private int num1;
  10.     public Text num1_tx;
  11.     private int num2;
  12.     public Text num2_tx;
  13.     public Text win_text;
  14.     // Use this for initialization
  15.     void Start()
  16.     {
  17.         min = 1;
  18.         max = 10;
  19.         num1 = Random.Range(min, max);
  20.         num1_tx.text = num1.ToString();
  21.     
  22.         win_text.text = "";
  23.     }
  24.     // Update is called once per frame
  25.     public void Onclick()
  26.     {
  27.         num2 = Random.Range(min, max);
  28.         num2_tx.text = num2.ToString();
  29.         if (num2 >= num1)
  30.         {
  31.             win_text.text = "成功!";
  32.         }
  33.         else
  34.         {
  35.             win_text.text = "失敗";
  36.         }
  37.     }
  38. }

 1から9の範囲でランダムに数字を出し、

最初の数字に対して、後のランダムな数字が大きければ成功、

小さければ失敗というもの。

本当はハイアンドローにしたいが、

上手く数値の参照ができなかった。

次の目標にしたい。

f:id:sashimimayonezu:20200817001845p:plain

見た目はこんな感じ

これができればUIだけのゲームができる。

https://help.hatenablog.com/entry/developer-option?_gl=1*ojnx55*_gcl_au*NzA5NzY1Mzc3LjE3MTA2NTgyODA.