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 JAdamBell · Nov 17, 2016 at 05:05 AM · scripting problemsceneloadlevel

Food and level in scavenger and 5.4.2f2

I'm having two issues with scavenger in 5.4.2f2

it is not keeping food between levels, and the levels are incrementing by 2. (I did the advised changes with SceneManager since loadlevel is depreciated)

Is there any advice?

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 
 public class Player : MovingObject {
 
     public int wallDamage = 1;
     public int pointsPerFood = 10;
     public int pointsPerSoda = 20;
     public float restarteLevelDelay = 1f;
     public Text foodText;
 
     private Animator animator;
     private int food;
 
     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;
 
         horizontal = (int)Input.GetAxisRaw("Horizontal");
         vertical = (int)Input.GetAxisRaw("Vertical");
 
         if (horizontal != 0)
             vertical = 0;
 
         if (horizontal != 0 || vertical != 0)
             AttemptMove<Wall>(horizontal, vertical);
     }
 
     private void OnTriggerEnter2D (Collider2D other)
     {
         if (other.tag == "Exit")
         {
             Invoke("Restart", restarteLevelDelay);
             enabled = false;
         }
 
         if (other.tag == "Food")
         {
             food += pointsPerFood;
             foodText.text = "+" + pointsPerFood + "Food: " + food;
             other.gameObject.SetActive(false);
         }
 
         if (other.tag == "Soda")
         {
             food += pointsPerSoda;
             foodText.text = "+" + pointsPerSoda + "Food: " + food;
             other.gameObject.SetActive(false);
         }
     }
     protected override void OnCantMove<T>(T componenet)
     {
         Wall hitWall = componenet as Wall;
         hitWall.DamageWall(wallDamage);
         animator.SetTrigger("playerChop");
     }
 
     private void Restart()
     {
         SceneManager.LoadScene(0);
     }
 
     public void LooseFood (int loss)
     {
         animator.SetTrigger("playerHit");
         food -= loss;
         foodText.text = "-" + loss + "Food: " + food;
         CheckIfGameOver();
     }
 
     protected override void AttemptMove <T> (int xDir, int yDir)
     {
         food--;
         foodText.text = "Food: " + food;
 
         base.AttemptMove<T>(xDir, yDir);
 
         RaycastHit2D hit;
 
         CheckIfGameOver();
 
         GameManager.instance.playersTurn = false;
     }
     private void CheckIfGameOver()
     {
         if (food <= 0)
             GameManager.instance.GameOver();
     }
 
  
 }

and

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class GameManager : MonoBehaviour
 {
     public float levelStartDelay = 2f;
     public float turnDelay = 0.1f;
     public static GameManager instance = null;        
     public int playerFoodPoints = 100;
     [HideInInspector] public bool playersTurn = true;
 
     private Text levelText;
     private GameObject levelImage;
     private BoardManager boardScript;   //Store a reference to our BoardManager which will set up the level.
     private int level = 1;
     private List<Enemy> enemies;
     private bool enemiesMoving;
     private bool doingSetup = true;
 
     //Awake is always called before any Start functions
     void Awake()
     {
         if (instance == null)
             instance = this;
         else if (instance != this)
             Destroy(gameObject);
 
         DontDestroyOnLoad(gameObject);
         enemies = new List<Enemy>();
         boardScript = GetComponent<BoardManager>();
         InitGame();
     }
 
     //Initializes the game for each level.
     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.text = "After " + level + " days, you starved.";
 
         levelImage.SetActive(true);
 
         enabled = false;
     }
 
     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;
     }
 
     //This is called each time a scene is loaded.
     void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
     {
         //Add one to our level number.
         level++;
         //Call InitGame to initialize our level.
         InitGame();
     }
     void OnEnable()
     {
         //Tell our ‘OnLevelFinishedLoading’ function to start listening for a scene change event as soon as this script is enabled.
         SceneManager.sceneLoaded += OnLevelFinishedLoading;
     }
 
     void OnDisable()
     {
         //Tell our ‘OnLevelFinishedLoading’ function to stop listening for a scene change event as soon as this script is disabled. //Remember to always have an unsubscription for every delegate you subscribe to!
         SceneManager.sceneLoaded -= OnLevelFinishedLoading;
     }
 }





Comment
Add comment · Show 1
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
avatar image hexagonius · Nov 17, 2016 at 11:42 PM 0
Share

yes, ins$$anonymous$$d of pasting everything you have, show the relevant parts for a quicker analysis

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

96 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Simple - Save and Loading Scene 0 Answers

Looking for someone to help me with my project 0 Answers

How to Reload a Scene but keeping others already Loaded Scenes?,How to Reaload a Scene and keep others Loaded Scenes without unload then? 2 Answers

Teleporting problems 1 Answer

example of new way to load levels 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