Hi again,
This is becoming a regular occurrence.
I’ve completed this section of the tutorial last night and began testing to ensure there’s no issues, and I came across one that I can’t quite figure out. When I drop an item in Scene A, and travel through the portal to Scene B, upon returning to Scene A the pickup is missing and I have four errors:
Can't remove Pickup (Script) because ClickablePickup (Script) depends on it.
I have a feeling it’s come about from implementing the changes discussed here: Pickup Spawner bug - Unity Courses / Ask - GameDev.tv or here: Another bug with the portals’ saving parts - Unity Courses / Talk - GameDev.tv
I’ll post my PickupSpawner.cs below:
using UnityEngine;
using RPG.Saving;
namespace GameDevTV.Inventories
{
/// <summary>
/// Spawns pickups that should exist on first load in a level. This
/// automatically spawns the correct prefab for a given inventory item.
/// </summary>
public class PickupSpawner : MonoBehaviour, ISaveable
{
// CONFIG DATA
[SerializeField] InventoryItem item = null;
[SerializeField] int number = 1;
// LIFECYCLE METHODS
private void Awake()
{
// Spawn in Awake so can be destroyed by save system after.
SpawnPickup();
}
// PUBLIC
/// <summary>
/// Returns the pickup spawned by this class if it exists.
/// </summary>
/// <returns>Returns null if the pickup has been collected.</returns>
public Pickup GetPickup()
{
return GetComponentInChildren<Pickup>();
}
/// <summary>
/// True if the pickup was collected.
/// </summary>
public bool hasBeenDestroyedByRestoreState = false;
public bool isCollected()
{
return GetPickup() == null || hasBeenDestroyedByRestoreState;
}
//PRIVATE
private void SpawnPickup()
{
var spawnedPickup = item.SpawnPickup(transform.position, number);
spawnedPickup.transform.SetParent(transform);
}
private void DestroyPickup()
{
var pickup = GetPickup();
if (pickup)
{
pickup.transform.SetParent(null);
Destroy(pickup);
hasBeenDestroyedByRestoreState = true;
}
}
object ISaveable.CaptureState()
{
return isCollected();
}
void ISaveable.RestoreState(object state)
{
bool shouldBeCollected = (bool)state;
if (shouldBeCollected && !isCollected())
{
DestroyPickup();
}
if (!shouldBeCollected && isCollected())
{
SpawnPickup();
}
}
}
}
Thanks!