Playable was not Disposed
I'm having issues with my program. I have the game set up to manipulate time as the users "dies", the character is then re-spawned and time starts back up again. However shortly after time resumes the game (the background, the playable, and obstacles) freezes. I am using Unity 5.4. I am programming this though a class on Lynda.com and my code matches my instructors. The only thing different is that I am getting the warning: Assets/Scripts/ObjectPool.cs(13) : Playable was not Disposed.
The warning is showing up under the console tab in Unity. I am not sure how to fix it, and I was wondering if anyone could help. I used Visual Studio to program.
Here is my code for my time manager. using UnityEngine; using System.Collections;
public class TimeManager : MonoBehaviour
{
public void ManipulateTime(float newTime, float duration)
{
if (Time.timeScale == 0)
Time.timeScale = 0.1f;
StartCoroutine(FadeTo(newTime, duration));
}
IEnumerator FadeTo(float value, float time)
{
for (float t = 0f; t < 1; t += Time.deltaTime / time)
{
Time.timeScale = Mathf.Lerp(Time.timeScale, value, t);
if (Mathf.Abs(value - Time.timeScale) < .01f)
{
Time.timeScale = value;
yield return false;
}
yield return null;
}
}
}
I also have code for my game manager which involves the time manager. using UnityEngine; using System.Collections;
public class GameManager : MonoBehaviour {
public GameObject playerPrefab;
private bool gameStarted;
private TimeManager timeManager;
private GameObject player;
private GameObject floor;
private Spawner spawner;
void Awake()
{
floor = GameObject.Find ("Foreground");
spawner = GameObject.Find ("Spawner").GetComponent<Spawner> ();
timeManager = GetComponent<TimeManager> ();
}
// Use this for initialization
void Start () {
var floorHeight = floor.transform.localScale.y;
var pos = floor.transform.position;
pos.x = 0;
pos.y = -((Screen.height / PixelPerfectCamera.pixelsToUnits) / 2) + (floorHeight / 2);
floor.transform.position = pos;
spawner.active = false;
Time.timeScale = 0;
}
// Update is called once per frame
void Update () {
if (!gameStarted && Time.timeScale == 0)
{
if (Input.anyKeyDown)
{
timeManager.ManipulateTime(1, 1f);
ResetGame();
}
}
}
void OnPlayerKilled()
{
spawner.active = false;
var playerDestroyScript = player.GetComponent<DestroyOffScreen>();
playerDestroyScript.DestoyCallback -= OnPlayerKilled;
player.GetComponent<Rigidbody2D>().velocity = Vector2.zero;
timeManager.ManipulateTime(0, 5.5f);
gameStarted = false;
}
void ResetGame()
{
spawner.active = true;
player = GameObjectUtil.Instantiate(playerPrefab, new Vector3(0, (Screen.height/PixelPerfectCamera.pixelsToUnits) / 2 +100, 0));
var playerDestroyScript = player.GetComponent<DestroyOffScreen> ();
playerDestroyScript.DestoyCallback += OnPlayerKilled;
gameStarted = true;
}
}
I would greatly appreciate any help. Thank you.
You posted two scripts but missed the one with the error (ObjectPool.cs)
Answer by PartyWolf · Oct 26, 2016 at 08:32 AM
Hey @storyteller2899,
Had the exact same problem today, my crazy programmer housemate just came up with this solution.
IEnumerator FadeTo(float value, float time) { for (float t = 0; t < 1; t += Time.deltaTime / time) {
Time.timeScale = Mathf.Lerp (Time.timeScale, value, t);
if(Mathf.Abs(value - Time.timeScale) < .01f) {
Time.timeScale = value;
break;
}
yield return null;
}
yield return false;
}
}
Blockquote
Hopefully this should work!
PS. I find this is a recurring thing in outdated lynda tutorials :P.