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裡的東西 // Start is called before the first frame update void Start() { Instance = this; // 指定Instance參考這個程式 } // Update is called once per frame void Update() { time += Time.deltaTime; //時間增加 if (time > 0.5f) //如果時間大於0.5(秒) { 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的內容 } }