Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Skippy1300 · Feb 09, 2016 at 02:17 PM · c#booleanapplication.loadlevelcondition

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 ();
         }
 
     }
 }
 
 


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

I'm trying to make the collision work. But... 2 Answers

Automatic Switching of two or more booleans in Inspector 1 Answer

error CS0266 Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?) 2 Answers

Calling Bool in Network Command Not Working 0 Answers

making a pause menu 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges