using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; //使用UI public class GameFunction : MonoBehaviour { public GameObject Enemy; //宣告物件,名稱Enemy public float time; //宣告浮點數,名稱time public Text ScoreText; // 宣告一個文字型態的變數 ScoreText public int Score = 0; // 宣告一個整數Score public static GameFunction Instance; // 設定Instance,讓其他程式能讀取GameFunction裡的東西 public GameObject GameTitle; // 宣告 GameTitle物件 public GameObject GameOverTitle; // 宣告 GameOverTitle物件 public GameObject PlayButton; // 宣告 PlayButton物件 public bool isPlaying = false; // 宣告 isPlaying的boolean變數,初始值false // Start is called before the first frame update void Start() { Instance = this; // 指定Instance參考這個程式 GameOverTitle.SetActive(false); // 設定GameOverTitle一開始不顯示 } // Update is called once per frame void Update() { time += Time.deltaTime; //時間增加 if (time > 0.5f && isPlaying == true) //如果時間大於0.5(秒) && isPlaying = True { Vector3 pos = new Vector3(Random.Range(-2.5f, 2.5f), 4.5f, 0); //宣告位置pos,Random.Range(-2.5f,2.5f)代表X是2.5到-2.5之間隨機 Instantiate(Enemy, pos, transform.rotation);//產生敵人 time = 0f; //時間歸零 } } public void AddScore() { Score += 10; //分數+10分 ScoreText.text = "Score: " + Score; //更新ScoreText的內容 } public void GameStart() { isPlaying = true; // 設定isPlaying = true,代表遊戲進行中 GameTitle.SetActive(false); // 不顯示GameTitle PlayButton.SetActive(false); // 不顯示PlayButton } }