Advertisement
JonneOpettaja

Level script C# Unity

Feb 16th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.SceneManagement;
  5.  
  6. public class Level : MonoBehaviour {
  7.  
  8. float startTime;
  9. int numTreasures;
  10. int collected = 0;
  11. bool levelComplete = false;
  12. GameObject player;
  13. public float levelBottomY = -10f;
  14. GameObject kamera;
  15.  
  16.  
  17. // Use this for initialization
  18. void Start () {
  19. startTime = Time.time;
  20. GameObject[] treasureObjects = GameObject.FindGameObjectsWithTag("Treasure");
  21. numTreasures = treasureObjects.Length;
  22. player = GameObject.FindWithTag("Player");
  23. Cursor.visible = false;
  24. kamera = GameObject.FindWithTag("MainCamera");
  25. }
  26.  
  27. // Update is called once per frame
  28. void Update () {
  29. if (player!= null && player.transform.position.y < levelBottomY)
  30. {
  31. Kill();
  32. }
  33. }
  34.  
  35. void Kill ()
  36. {
  37. kamera.transform.parent = null;
  38. GameObject.Destroy(player);
  39. levelComplete = true;
  40. }
  41.  
  42.  
  43. void TreasureCollected()
  44. {
  45. collected++;
  46. if (collected == numTreasures)
  47. {
  48. levelComplete = true;
  49. // GameObject hiscores = GameObject.Find("Hiscores");
  50. // if (hiscores != null)
  51. // {
  52. // hiscores.GetComponent<Hiscores>().StoreTime(SceneManager.GetActiveScene().buildIndex, Time.time - startTime);
  53. // }
  54. }
  55. }
  56.  
  57. bool LevelEndMessage(string message, string buttonText)
  58. {
  59. Cursor.visible = true;
  60. GUI.skin.label.alignment = TextAnchor.MiddleCenter;
  61. GUI.Label(new Rect(Screen.width / 2 - 200 / 2, Screen.height / 2 -75, 200, 50), message);
  62. return GUI.Button(new Rect(Screen.width / 2 - 200 / 2, Screen.height / 2 + 25, 200, 50), buttonText);
  63. }
  64.  
  65.  
  66. private void OnGUI()
  67. {
  68. GUI.skin.label.fontSize = 20;
  69. GUI.skin.label.alignment = TextAnchor.MiddleLeft;
  70. GUI.skin.label.alignment = TextAnchor.MiddleLeft;
  71. GUI.Label(new Rect(10, 10, 200, 30), "Time: " + (Time.time - startTime).ToString("#.##"));
  72. GUI.Label(new Rect(10, 40, 200, 30), "Treasures: " + collected + "/" + numTreasures);
  73.  
  74. if (levelComplete)
  75.  
  76. {
  77. if (player != null)
  78. {
  79. if (LevelEndMessage("Level completed!", "Continue"))
  80. {
  81. int nextLevel = SceneManager.GetActiveScene().buildIndex + 1;
  82. if (nextLevel >= SceneManager.sceneCountInBuildSettings)
  83. { nextLevel = 0; }
  84. SceneManager.LoadScene(nextLevel);
  85. }
  86. }
  87.  
  88. else if (player == null)
  89. {
  90. if (LevelEndMessage("Level failed", "Retry"))
  91. {
  92. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
  93. }
  94. if (GUI.Button(new Rect(Screen.width / 2 - 200 / 2, Screen.height / 2 + 100, 200, 50)
  95. , "Give up"))
  96. {
  97. SceneManager.LoadScene(0);
  98. }
  99. }
  100. }
  101. }
  102.  
  103.  
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement