Can someone tell me whats missing on my script for resetting collected stars after completing level?
I am trying to make my collected stars reset after a level is completed. I now have it to where the score and the stars reset upon player death, However when the level is completed the star count remains upon entering next level. If anyone could help, I'd really appreciate it. `
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class LevelManager : MonoBehaviour
{
public static LevelManager Instance { get; private set; }
public Player Player { get; private set; }
public CameraController Camera { get; private set; }
public TimeSpan RunningTime { get { return DateTime.UtcNow - _started; } }
public int CurrentTimeBonus
{
get
{
var secondDifference = (int) (BonusCutoffSeconds - RunningTime.TotalSeconds);
return Mathf.Max (0, secondDifference) * BonusSecondMultiplier;
}
}
private List<Checkpoint> _checkpoints;
private int _currentCheckpointIndex;
private DateTime _started;
private int _savedPoints;
private int _starsCollected;
public Checkpoint DebugSpawn;
public int BonusCutoffSeconds;
public int BonusSecondMultiplier;
public void Awake()
{
_savedPoints = GameManager.Instance.Points;
_starsCollected = GameManager.Instance.Stars;
Instance = this;
}
public void Start()
{
_checkpoints = FindObjectsOfType<Checkpoint> ().OrderBy (t => t.transform.position.x).ToList ();
_currentCheckpointIndex = _checkpoints.Count > 0 ? 0 : -1;
Player = FindObjectOfType<Player> ();
Camera = FindObjectOfType<CameraController> ();
_started = DateTime.UtcNow;
var listeners = FindObjectsOfType<MonoBehaviour> ().OfType<IPlayerRespawnListener> ();
foreach (var listener in listeners)
{
for (var i = _checkpoints.Count - 1; i >= 0; i --)
{
var distance = ((MonoBehaviour)listener).transform.position.x - _checkpoints[i].transform.position.x;
if(distance < 0)
continue;
_checkpoints[i].AssignObjectToCheckpoint (listener);
break;
}
}
#if UNITY_EDITOR
if (DebugSpawn != null)
DebugSpawn.SpawnPlayer (Player);
else if (_currentCheckpointIndex != -1)
_checkpoints [_currentCheckpointIndex].SpawnPlayer (Player);
#else
#endif
}
public void Update()
{
var isAtLastCheckpoint = _currentCheckpointIndex + 1 >= _checkpoints.Count;
if (isAtLastCheckpoint)
return;
var distanceToNextCheckpoint = _checkpoints [_currentCheckpointIndex + 1].transform.position.x - Player.transform.position.x;
if (distanceToNextCheckpoint >= 0)
return;
_checkpoints [_currentCheckpointIndex].PlayerLeftCheckpoint ();
_currentCheckpointIndex++;
_checkpoints [_currentCheckpointIndex].PlayerHitCheckpoint ();
GameManager.Instance.AddPoints (CurrentTimeBonus);
_savedPoints = GameManager.Instance.Points;
_starsCollected = GameManager.Instance.Stars;
_started = DateTime.UtcNow;
}
public void GotoNextLevel(string levelName)
{
StartCoroutine (GotoNextLevelCo (levelName));
}
private IEnumerator GotoNextLevelCo(string levelName)
{
Player.FinishLevel();
GameManager.Instance.AddPoints(CurrentTimeBonus);
FloatingText.Show ("Level Complete!", "CheckpointText", new CenteredTextPositioner (.2f));
yield return new WaitForSeconds (1);
FloatingText.Show(string.Format("{0} points!", GameManager.Instance.Points), "CheckpointText", new CenteredTextPositioner(.1f));
yield return new WaitForSeconds(3f);
if (string.IsNullOrEmpty(levelName))
Application.LoadLevel("StartScreen");
else
Application.LoadLevel(levelName);
}
public void KillPlayer()
{
StartCoroutine (KillPLayerCo ());
}
private IEnumerator KillPLayerCo()
{
Player.Kill ();
Camera.IsFollowing = false;
yield return new WaitForSeconds (2f);
Camera.IsFollowing = true;
if (_currentCheckpointIndex != -1)
_checkpoints [_currentCheckpointIndex].SpawnPlayer (Player);
_started = DateTime.UtcNow;
GameManager.Instance.ResetPoints (_savedPoints, _starsCollected);
}
}
Answer by Maurice_B · Feb 20, 2016 at 01:03 PM
Hi, could you just show where the code to finish level is?
public void GotoNextLevel(string levelName) { StartCoroutine (GotoNextLevelCo (levelName)); }
private IEnumerator GotoNextLevelCo(string levelName)
{
Player.FinishLevel();
Game$$anonymous$$anager.Instance.AddPoints(CurrentTimeBonus);
FloatingText.Show ("Level Complete!", "CheckpointText", new CenteredTextPositioner (.2f));
yield return new WaitForSeconds (1);
FloatingText.Show(string.Format("{0} points!", Game$$anonymous$$anager.Instance.Points), "CheckpointText", new CenteredTextPositioner(.1f));
yield return new WaitForSeconds(3f);
if (string.IsNullOrEmpty(levelName))
Application.LoadLevel("StartScreen");
else
Application.LoadLevel(levelName);
}
SOLVED.. I was putting Game$$anonymous$$anager.Instance.ResetPoints (starsCollected); in the wrong location.
Your answer
Follow this Question
Related Questions
Ball reset in Pong 1 Answer
Reset Raw UI Image For Audio Spectrum Data 0 Answers
Getting Hit direction to show hit indicator 1 Answer
2D Power up script not working 0 Answers
2D Power up script not working 0 Answers