I have a set up like this
public class Enemy : MonoBehaviour {
public Weapon weapon;
....
}
public abstract class Weapon : MonoBehaviour { ... }
public class ClawWeapon : Weapon { ... }
public class EnemySpawner {
...
GameObject enemy = (GameObject) GameObject.Instantiate(Resources.Load("enemy"));
}
My enemy prefab asset has a claw weapon prefab in its Weapon field in the inspector. However, when I instantiate it and place it into my scene with my EnemySpawner, the Weapon field is null. I would have expected Instantiate to fully instantiate the prefab, including sub objects, but that doesn’t seem to be happening. I can Instantiate the weapon as part of Start()
in my Enemy class, but it would be nice to be able to configure it quickly by swapping weapons in the editor and prefab for prototyping.
Am I doing something wrong to cause the Weapon field to not be instantiated? I’ve read unity has weird interactions with abstract classes sometimes, maybe thats affecting this? Or is there a better way to do this?