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
yes, ins$$anonymous$$d of pasting everything you have, show the relevant parts for a quicker analysis
Your answer
![](https://koobas.hobune.stream/wayback/20220612100835im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Simple - Save and Loading Scene 0 Answers
Looking for someone to help me with my project 0 Answers
Teleporting problems 1 Answer
example of new way to load levels 1 Answer