Deleting NPC after leaving a Scene

So I guess this is the last question I have, in order to finish my RPG Game.

When I am leaving the first Scene, I wanted to delete a NPC forever from the Scene, cause he is moving as a “Mentor” with me in the second scene. So I made a UnityEvent on the LevelPortal to trigger a Function from a Script that lays on the NPC itself and that is setting a bool and Capture the state.

public class DestroySelf : MonoBehaviour,ISaveable
{
    bool isGone;

    public void Gone()
    {
        isGone = true;
        
    }


       public object CaptureState()
        {
           
            return isGone;
        }

        public void RestoreState(object state)
        { 
            if ((bool)state)
            {
                Destroy(gameObject);
            }
        }

}

So the Capture State is working alright and doing his thing but when I am going back to the first Scene, the NPC is still there and Restore State won’t be even called and I am asking myself why? With other things like Fighter.cs its all working. And yes, the NPC has a Saveable Entetity and everything that is needed.

Is it possible that you’re calling OnGo() after the Save?

There is a structural issue with this scenario, even if it did work…
Scene 1: OnGo() tells Jarl - TheMentor that he’s no longer needed.when moving to Scene 2
Scene 2: Play as usual, go back to Scene 1
Scene 1: RestoreState() tells Jarl he’s toast and deletes him. Now go back to Scene 2 and…
OnGo tries to invoke DestroySelf.Gone() on Jarl, but Jarl no longer exists, the script throws a null reference error and quits working, the game hangs…

A more robust solution is to create a Spawner for Jarl. Link the OnGo() event to the Spawner…

In Awake(), Jarl is spawned at the starting location. Jarl’s Instance is cached.
OnGo will link to the Spawner’s DestroySelf.Gone() (maybe call it DestroySpawnedObject?)
The Spawner will get the SaveableEntity component.
CaptureState captures the bool as you’ve got
RestoreState destroys the spawned Instance if the bool was returned.

Again, though… it depends on OnGo being called at the right time…
Perhaps a Debug in CaptureState() and RestoreState() indicating what the state was when capturing or restoring might shed some light on the issue.

Privacy & Terms