how to keep an animation condition after Application.loadLevel
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.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
public float levelStartDelay = 2f;
public float turnDelay = .1f;
public static GameManager instance = null;
public BoardManager boardScript;
public int playerFoodPoints = 100;
[HideInInspector] public bool playersTurn = true;
private Text levelText;
private GameObject levelImage;
private int level = 1;
private List<Enemy> enemies;
private bool enemiesMoving;
private bool doingSetup;
public Animator animator;
// Use this for initialization
void Awake ()
{
if (instance == null)
instance = this;
else if (instance != this)
Destroy (gameObject);
DontDestroyOnLoad (gameObject);
enemies = new List<Enemy> ();
boardScript = GetComponent<BoardManager> ();
InitGame ();
}
private void OnLevelWasLoaded (int index) {
level++;
InitGame ();
}
void InitGame()
{
doingSetup = true;
levelImage = GameObject.Find ("LevelImage");
levelText = GameObject.Find ("LevelText").GetComponent<Text> ();
levelText.text = "Day " + level;
levelImage.SetActive (true);
Invoke ("HideLevelImage", levelStartDelay);
enemies.Clear ();
boardScript.SetupScene (level);
}
private void HideLevelImage () {
levelImage.SetActive (false);
doingSetup = false;
}
public void GameOver () {
levelText.fontSize = 24;
levelText.text = "You met your end after " + level + " days.";
levelImage.SetActive (true);
enabled = false;
}
// Update is called once per frame
void Update () {
if (playersTurn || enemiesMoving || doingSetup)
return;
StartCoroutine (MoveEnemies ());
}
public void AddEnemyToList(Enemy script) {
enemies.Add (script);
}
IEnumerator MoveEnemies() {
enemiesMoving = true;
yield return new WaitForSeconds(turnDelay);
if (enemies.Count == 0) {
yield return new WaitForSeconds(turnDelay);
}
for (int i = 0; i < enemies.Count; i++) {
enemies[i].MoveEnemy ();
yield return new WaitForSeconds (enemies[i].moveTime);
}
playersTurn = true;
enemiesMoving = false;
}
}
Here is my Player.cs file.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Player : MovingObject {
public int wallDamage = 1;
public int pointsPerFood = 10;
public int pointsPerSoda = 20;
public float restartLevelDelay = 1f;
public Text foodText;
public AudioClip moveSound1;
public AudioClip moveSound2;
public AudioClip eatSound1;
public AudioClip eatSound2;
public AudioClip drinkSound1;
public AudioClip drinkSound2;
public AudioClip gameOverSound;
public AudioClip shotgunPickup;
private Animator animator;
private int food;
private Vector2 touchOrigin = -Vector2.one;
// Use this for initialization
protected override void Start () {
animator = GetComponent<Animator> ();
food = GameManager.instance.playerFoodPoints;
foodText.text = "Food: " + food;
base.Start ();
}
private void OnDisable() {
GameManager.instance.playerFoodPoints = food;
}
// Update is called once per frame
void Update () {
if (!GameManager.instance.playersTurn) return;
int horizontal = 0;
int vertical = 0;
#if UNITY_STANDALONE || UNITY_WEBPLAYER
horizontal = (int)Input.GetAxisRaw ("Horizontal");
vertical = (int)Input.GetAxisRaw ("Vertical");
if (horizontal != 0)
vertical = 0;
if (horizontal != 0 || vertical != 0)
AttemptMove<Wall> (horizontal, vertical);
#else
if (Input.touchCount > 0) {
Touch myTouch = Input.touches [0];
if (myTouch.phase == TouchPhase.Began) {
touchOrigin = myTouch.position;
}
else if (myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0) {
Vector2 touchEnd = myTouch.position;
float x = touchEnd.x - touchOrigin.x;
float y = touchEnd.y - touchOrigin.y;
touchOrigin.x = -1;
if (Mathf.Abs (x) > Mathf.Abs (y))
horizontal = x > 0 ? 1 : -1;
else
vertical = y > 0 ? 1 : -1;
}
}
#endif
}
protected override void AttemptMove <T> (int xDir, int yDir) {
food --;
foodText.text = "Food: " + food;
base.AttemptMove <T> (xDir, yDir);
RaycastHit2D hit;
if (Move (xDir, yDir, out hit)) {
SoundManager.instance.RandomizeSfx (moveSound1, moveSound2);
}
CheckIfGameOver ();
GameManager.instance.playersTurn = false;
}
private void OnTriggerEnter2D (Collider2D other) {
if (other.tag == "Exit") {
Invoke ("Restart", restartLevelDelay);
enabled = false;
} else if (other.tag == "Food") {
food += pointsPerFood;
foodText.text = "+" + pointsPerFood + " Food: " + food;
SoundManager.instance.RandomizeSfx (eatSound1, eatSound2);
other.gameObject.SetActive (false);
} else if (other.tag == "Soda") {
food += pointsPerSoda;
foodText.text = "+" + pointsPerSoda + " Food: " + food;
SoundManager.instance.RandomizeSfx (drinkSound1, drinkSound2);
other.gameObject.SetActive (false);
} else if (other.tag == "Shotgun") {
animator.SetBool ("playerShotgun", true);
other.gameObject.SetActive (false);
SoundManager.instance.RandomizeSfx (shotgunPickup);
}
}
protected override void OnCantMove <T> (T component) {
Wall hitWall = component as Wall;
hitWall.DamageWall (wallDamage);
animator.SetTrigger ("playerChop");
}
private void Restart () {
Application.LoadLevel (Application.loadedLevel);
}
public void LoseFood (int loss) {
animator.SetTrigger ("playerHit");
food -= loss;
foodText.text = "-" + loss + " Food: " + food;
CheckIfGameOver ();
}
private void CheckIfGameOver() {
if (food <= 0) {
SoundManager.instance.PlaySingle (gameOverSound);
SoundManager.instance.musicSource.Stop ();
GameManager.instance.GameOver ();
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612063359im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I'm trying to make the collision work. But... 2 Answers
Automatic Switching of two or more booleans in Inspector 1 Answer
Calling Bool in Network Command Not Working 0 Answers
making a pause menu 1 Answer