Search Unity

Trouble expanding upon the Roguelike 2D tutorial...

Discussion in '2D' started by Skippy1300, Feb 7, 2016.

  1. Skippy1300

    Skippy1300

    Joined:
    Feb 7, 2016
    Posts:
    3
    Hello everyone! This is my first post. :D

    So I finished the Roguelike 2D tutorial completely and decided to test my knowledge by expanding upon the finished product. Currently I've created a shotgun that has a 10% chance of spawning whenever a level is loaded. Ive made an animation for the player so when he picks up the shotgun, his idle animation turns into a new idle animation of him holding the shotgun. This all works fine until I advance the level.

    There is a boolean condition that decides what animation is playing, and when the game reloads the scene, the boolean resets itself. I've spent countless hours and different attempts to try and keep the boolean variable past the level exit but to no avail. Can somebody please tell me how to keep that data through the Restart function?

    Here is the GameManager.cs file.

    Code (CSharp):
    1. sing UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Player : MovingObject {
    6.  
    7.     public int wallDamage = 1;
    8.     public int pointsPerFood = 10;
    9.     public int pointsPerSoda = 20;
    10.     public float restartLevelDelay = 1f;
    11.     public Text foodText;
    12.  
    13.  
    14.  
    15.     public AudioClip moveSound1;
    16.     public AudioClip moveSound2;
    17.     public AudioClip eatSound1;
    18.     public AudioClip eatSound2;
    19.     public AudioClip drinkSound1;
    20.     public AudioClip drinkSound2;
    21.     public AudioClip gameOverSound;
    22.  
    23.     public AudioClip shotgunPickup;
    24.  
    25.     private Animator animator;
    26.     private int food;
    27.    
    28.  
    29.     private Vector2 touchOrigin = -Vector2.one;
    30.  
    31.     // Use this for initialization
    32.     protected override void Start () {
    33.  
    34.         animator = GetComponent<Animator> ();
    35.  
    36.         food = GameManager.instance.playerFoodPoints;
    37.  
    38.         foodText.text = "Food: " + food;
    39.  
    40.         base.Start ();
    41.    
    42.     }
    43.  
    44.     private void OnDisable() {
    45.  
    46.         GameManager.instance.playerFoodPoints = food;
    47.     }
    48.    
    49.     // Update is called once per frame
    50.     void Update () {
    51.  
    52.         if (!GameManager.instance.playersTurn) return;
    53.  
    54.         int horizontal = 0;
    55.         int vertical = 0;
    56.  
    57. #if UNITY_STANDALONE || UNITY_WEBPLAYER
    58.  
    59.         horizontal = (int)Input.GetAxisRaw ("Horizontal");
    60.         vertical = (int)Input.GetAxisRaw ("Vertical");
    61.  
    62.         if (horizontal != 0)
    63.             vertical = 0;
    64.  
    65.         if (horizontal != 0 || vertical != 0)
    66.             AttemptMove<Wall> (horizontal, vertical);
    67.  
    68. #else
    69.  
    70.         if (Input.touchCount > 0) {
    71.  
    72.             Touch myTouch = Input.touches [0];
    73.  
    74.             if (myTouch.phase == TouchPhase.Began) {
    75.                 touchOrigin = myTouch.position;
    76.            
    77.             }
    78.  
    79.             else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0) {
    80.            
    81.                 Vector2 touchEnd = myTouch.position;
    82.                 float x = touchEnd.x - touchOrigin.x;
    83.                 float y = touchEnd.y - touchOrigin.y;
    84.                 touchOrigin.x = -1;
    85.                 if (Mathf.Abs (x) > Mathf.Abs (y))
    86.                     horizontal = x > 0 ? 1 : -1;
    87.                 else
    88.                     vertical = y > 0 ? 1 : -1;
    89.            
    90.             }
    91.  
    92.         }
    93.  
    94. #endif
    95.  
    96.  
    97.     }
    98.  
    99.     protected override void AttemptMove <T> (int xDir, int yDir) {
    100.    
    101.         food --;
    102.         foodText.text = "Food: " + food;
    103.  
    104.         base.AttemptMove <T> (xDir, yDir);
    105.  
    106.         RaycastHit2D hit;
    107.         if (Move (xDir, yDir, out hit)) {
    108.             SoundManager.instance.RandomizeSfx (moveSound1, moveSound2);
    109.        
    110.         }
    111.  
    112.         CheckIfGameOver ();
    113.  
    114.         GameManager.instance.playersTurn = false;
    115.     }
    116.  
    117.     private void OnTriggerEnter2D (Collider2D other) {
    118.  
    119.         if (other.tag == "Exit") {
    120.             Invoke ("Restart", restartLevelDelay);
    121.             enabled = false;
    122.         } else if (other.tag == "Food") {
    123.             food += pointsPerFood;
    124.             foodText.text = "+" + pointsPerFood + " Food: " + food;
    125.             SoundManager.instance.RandomizeSfx (eatSound1, eatSound2);
    126.             other.gameObject.SetActive (false);
    127.         } else if (other.tag == "Soda") {
    128.             food += pointsPerSoda;
    129.             foodText.text = "+" + pointsPerSoda + " Food: " + food;
    130.             SoundManager.instance.RandomizeSfx (drinkSound1, drinkSound2);
    131.             other.gameObject.SetActive (false);
    132.         } else if (other.tag == "Shotgun") {
    133.             animator.SetBool ("playerShotgun", true);
    134.             other.gameObject.SetActive (false);
    135.             SoundManager.instance.RandomizeSfx (shotgunPickup);
    136.         }
    137.     }
    138.     protected override void OnCantMove <T> (T component) {
    139.  
    140.         Wall hitWall = component as Wall;
    141.         hitWall.DamageWall (wallDamage);
    142.         animator.SetTrigger ("playerChop");
    143.     }
    144.  
    145.     private void Restart () {
    146.  
    147.         Application.LoadLevel (Application.loadedLevel);
    148.  
    149.     }
    150.  
    151.     public void LoseFood (int loss) {
    152.  
    153.         animator.SetTrigger ("playerHit");
    154.         food -= loss;
    155.         foodText.text = "-" + loss + " Food: " + food;
    156.         CheckIfGameOver ();
    157.     }
    158.  
    159.  
    160.     private void CheckIfGameOver() {
    161.  
    162.         if (food <= 0) {
    163.             SoundManager.instance.PlaySingle (gameOverSound);
    164.             SoundManager.instance.musicSource.Stop ();
    165.             GameManager.instance.GameOver ();
    166.         }
    167.  
    168.     }
    169. }
    Here is my Player.cs file.
    Code (CSharp):
    1.  
    2. usingUnityEngine;
    3. usingSystem.Collections;
    4. usingUnityEngine.UI;
    5.  
    6. publicclassPlayer : MovingObject {
    7.  
    8. publicintwallDamage = 1;
    9. publicintpointsPerFood = 10;
    10. publicintpointsPerSoda = 20;
    11. publicfloatrestartLevelDelay = 1f;
    12. publicTextfoodText;
    13.  
    14.  
    15.  
    16. publicAudioClipmoveSound1;
    17. publicAudioClipmoveSound2;
    18. publicAudioClipeatSound1;
    19. publicAudioClipeatSound2;
    20. publicAudioClipdrinkSound1;
    21. publicAudioClipdrinkSound2;
    22. publicAudioClipgameOverSound;
    23.  
    24. publicAudioClipshotgunPickup;
    25.  
    26. privateAnimatoranimator;
    27. privateintfood;
    28.  
    29.  
    30. privateVector2touchOrigin = -Vector2.one;
    31.  
    32. //Usethisforinitialization
    33. protectedoverridevoidStart () {
    34.  
    35. animator = GetComponent<Animator> ();
    36.  
    37. food = GameManager.instance.playerFoodPoints;
    38.  
    39. foodText.text = "Food: " + food;
    40.  
    41. base.Start ();
    42.  
    43.  }
    44.  
    45. privatevoidOnDisable() {
    46.  
    47. GameManager.instance.playerFoodPoints = food;
    48.  }
    49.  
    50. //Updateiscalledonceperframe
    51. voidUpdate () {
    52.  
    53. if (!GameManager.instance.playersTurn) return;
    54.  
    55. inthorizontal = 0;
    56. intvertical = 0;
    57.  
    58. #ifUNITY_STANDALONE || UNITY_WEBPLAYER
    59.  
    60. horizontal = (int)Input.GetAxisRaw ("Horizontal");
    61. vertical = (int)Input.GetAxisRaw ("Vertical");
    62.  
    63. if (horizontal != 0)
    64. vertical = 0;
    65.  
    66. if (horizontal != 0 || vertical != 0)
    67. AttemptMove<Wall> (horizontal, vertical);
    68.  
    69. #else
    70.  
    71. if (Input.touchCount > 0) {
    72.  
    73. TouchmyTouch = Input.touches [0];
    74.  
    75. if (myTouch.phase == TouchPhase.Began) {
    76. touchOrigin = myTouch.position;
    77.  
    78.  }
    79.  
    80. elseif (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0) {
    81.  
    82. Vector2touchEnd = myTouch.position;
    83. floatx = touchEnd.x - touchOrigin.x;
    84. floaty = touchEnd.y - touchOrigin.y;
    85. touchOrigin.x = -1;
    86. if (Mathf.Abs (x) > Mathf.Abs (y))
    87. horizontal = x > 0 ? 1 : -1;
    88. else
    89. vertical = y > 0 ? 1 : -1;
    90.  
    91.  }
    92.  
    93.  }
    94.  
    95. #endif
    96.  
    97.  
    98.  }
    99.  
    100. protectedoverridevoidAttemptMove <T> (intxDir, intyDir) {
    101.  
    102. food --;
    103. foodText.text = "Food: " + food;
    104.  
    105. base.AttemptMove <T> (xDir, yDir);
    106.  
    107. RaycastHit2Dhit;
    108. if (Move (xDir, yDir, outhit)) {
    109. SoundManager.instance.RandomizeSfx (moveSound1, moveSound2);
    110.  
    111.  }
    112.  
    113. CheckIfGameOver ();
    114.  
    115. GameManager.instance.playersTurn = false;
    116.  }
    117.  
    118. privatevoidOnTriggerEnter2D (Collider2Dother) {
    119.  
    120. if (other.tag == "Exit") {
    121. Invoke ("Restart", restartLevelDelay);
    122. enabled = false;
    123.  } elseif (other.tag == "Food") {
    124. food += pointsPerFood;
    125. foodText.text = "+" + pointsPerFood + " Food: " + food;
    126. SoundManager.instance.RandomizeSfx (eatSound1, eatSound2);
    127. other.gameObject.SetActive (false);
    128.  } elseif (other.tag == "Soda") {
    129. food += pointsPerSoda;
    130. foodText.text = "+" + pointsPerSoda + " Food: " + food;
    131. SoundManager.instance.RandomizeSfx (drinkSound1, drinkSound2);
    132. other.gameObject.SetActive (false);
    133.  } elseif (other.tag == "Shotgun") {
    134. animator.SetBool ("playerShotgun", true);
    135. other.gameObject.SetActive (false);
    136. SoundManager.instance.RandomizeSfx (shotgunPickup);
    137.  }
    138.  }
    139.  
    140. protectedoverridevoidOnCantMove <T> (Tcomponent) {
    141.  
    142. WallhitWall = componentasWall;
    143. hitWall.DamageWall (wallDamage);
    144. animator.SetTrigger ("playerChop");
    145.  }
    146.  
    147. privatevoidRestart () {
    148.  
    149. Application.LoadLevel (Application.loadedLevel);
    150.  
    151.  }
    152.  
    153. publicvoidLoseFood (intloss) {
    154.  
    155. animator.SetTrigger ("playerHit");
    156. food -= loss;
    157. foodText.text = "-" + loss + " Food: " + food;
    158. CheckIfGameOver ();
    159.  }
    160.  
    161.  
    162. privatevoidCheckIfGameOver() {
    163.  
    164. if (food <= 0) {
    165. SoundManager.instance.PlaySingle (gameOverSound);
    166. SoundManager.instance.musicSource.Stop ();
    167. GameManager.instance.GameOver ();
    168.  }
    169.  
    170.  }
    171. }
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    1. try PlayerPrefs. There no booleans, you can use int. Somewhere in you level (befor next level is loaded): if (yourbool) PlayerPrefs.SetInt ("boolName", 1); Then if you start the next level, somewhere in start() (or awake): if (PlayerPrefs.GetInt ("boolName" ==1) yourbool = true;
    2. try DontDestroyOnLoad()
    http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
    I think, playerPrefs is better.
     
    Skippy1300 likes this.
  3. Skippy1300

    Skippy1300

    Joined:
    Feb 7, 2016
    Posts:
    3
    Your sir are simply amazing.. I never even knew about PlayerPrefs!!! Thank you so much!!