【Unity5】uGUIでタイトルとゲームオーバー画面を実装!Canvas, Text, Button【フラッピーバード風5】
↑
こちらのサイトを参考にIsTriggerがOFになっているオブジェクトに当たるとゲームオーバーになり、GameOverTextが表示されるシーンを作成しました。
しかし、オブジェクトに当たってもゲームオーバーにならず困っています。
ご教授願います。
c#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class CubeController : MonoBehaviour { 6 public AudioClip Point; 7 private AudioSource audioSource1; 8 private Rigidbody2D rb2D; 9 private float jumpForce = 4.0f; 10 public GameObject Score; 11 public GameObject GameOverGUI; 12 private bool gameover = false; 13 14 // Use this for initialization 15 private void Start () { 16 rb2D = GetComponent<Rigidbody2D>(); 17 } 18 19 void Update () { 20 //gameoverフラグがtrueになった時、ジャンプできないようにする 21 if(Input.GetMouseButtonDown(0) && !gameover){ 22 Jump(); 23 } 24 if(gameover == true){ 25 //GAME OVERのGUIを表示する 26 GameOverGUI.SendMessage("Lose"); 27 } 28 } 29 30 void Jump(){ 31 rb2D.velocity = new Vector2(rb2D.velocity.x, jumpForce); 32 } 33 34 private void OnCollisionEnter2D (Collision2D collision) { 35 StartCoroutine(GameOver()); 36 } 37 38 void OnTriggerEnter2D (Collider2D col){ 39 audioSource1 = gameObject.GetComponent<AudioSource> (); 40 audioSource1.clip = Point; 41 audioSource1.Play (); 42 //Score(GUIText)のScoreUpメソッドを呼び出す 43 Score.SendMessage ("ScoreUp", 1); 44 //何度も加点されないように削除しておく 45 Destroy (col.gameObject); 46 } 47 48 IEnumerator GameOver() { 49 // ゲームオーバーのフラグをtrueにする 50 gameover = true; 51 // マウス連打してたらスコアを見る暇もなくタイトルへ戻ってしまう対策、1秒待機 52 yield return new WaitForSeconds(1f); 53 // マウスクリックしたらゲームの最初に戻る 54 while(!Input.GetMouseButtonDown(0)) { yield return 0; } 55 Application.LoadLevel("TitleScene"); 56 } 57}
c#
1using UnityEngine; 2using UnityEngine.UI; 3using System.Collections; 4 5public class gameOverScript : MonoBehaviour { 6 7 void Start () { 8 gameObject.GetComponent<Text>().enabled = false; 9 } 10 11 public void Lose () { 12 gameObject.GetComponent<Text>().enabled = true; 13 } 14}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/02 01:30
退会済みユーザー
2018/07/02 10:40 編集