Weapon Spawn() and Stage Select

Hello, I’m reworking my way through the RPG Core Combat course but modifying it as I go along to make a game more akin to the original Battleheart (most pertinently here is it uses a stage select system rather than a traversable path between different screens and the player can (optionally) control multiple characters).

For the MVP version, I have each stage (separate scenes) pre-seeded with one or more player character prefabs and an enemy spawner that spawns pre-defined waves of enemies. This works fine if I play each scene from within the scene itself, but I have just hooked up the stage select screen and am encountering a problem:

When I load into a new stage scene from the stage select, the pre-seeded player prefab(s) as well as the instantiated enemy prefabs are not spawning their Weapon gameobjects. I’ve been able to verify that the weapon Spawn method is running, but the weapon prefab is reading as null. Looking at the player and enemy gameobjects in the inspector, I can also see that their weapon prefab in the Fighter class is null as well.

So to restate, if I play the scene from within the scene normally, all of the weapon prefabs are showing up normally and instantiated enemies are also spawning their weapons, but if I enter the scene through LoadScene(), the character prefabs already in the scene as well as the instantiated enemy prefabs are listing their weapon prefab in Fighter.cs as null and therefore not spawning any weapon.

I’m wondering what I can do to begin investigating this? I’m not sure where the error might be since it seems to be happening between scene loads and everything else is working fine. I can post screenshots and script snippets later if necessary, but right now I’m more interested in what parts of the code or scene loading/unloading process I can investigate or research further to help me find the error myself.

Thanks a lot!

The first question I have is (and just trust me, I ask this question a lot), Are there any error messages?

The other thing to look at is to make sure that all of your WeaponConfigs (or Weapons, depending on how far into the course you are) are in a folder named Resources (spelling and capitalization matters). It’s also important to note here that Assets/Game/Weapons/Resources works, but Assets/Game/Resources/Weapons will not work.

So let’s add some debugs to RestoreState() in Fighter.cs to see if we can get a glimpse of what’s happening:

public void RestoreState(object state)
            string weaponName = (string)state;
            Debug.Log($"{name} is restoring weapon {weaponName}");
            Weapon weapon = Resources.Load<Weapon>(weaponName);
                  Debug.Log($"{name}'s {weaponName} was not found in a Resources folder");
                  return;  //at least don't lose the weapon we started with
            } else
                  Debug.Log($"{name} successfully loaded {weapon.name}");

Thanks for the response! I’ve had the saving and restoring code commented out for a while now, I’m sure I’ll have a lot of questions when I reenable that (once I have characters start earning XP and saving that between stages).

Oddly enough all it took to get it working was to quit and restart Unity, so that’s both frustrating and a relief.