- Home /
Non-Monobehaviour data member not being serialised
I have a gun with a Barrel class. A gun can have lots of barrels. Each barrel can shoot projectiles in a variety of formations. I want one prototype barrel that all other barrels are cloned from. Let's say the game starts and the player picks up a gun with one barrel that only shoots one projectile straight ahead. Later he gets an upgrade that increases both the number of barrels on the gun and the number of projectiles each barrels fires. I want it so the gun has just one prototype barrel that is cloned when the player picks up an item that adds an additional barrel to the gun. Here's where the problem occurs. My barrel class looks like this.
public class Barrel : MonoBehaviour
{
[SerializeField] public Formation projectileFormation_;
[SerializeField] int numProjectiles_;
...
}
(I added the "public" and "SerializeField" part trying to fix this problem but it hasn't worked yet. ). Here's where the actual cloning takes place in a separate class.
void AddBarrel(Barrel prototype)
{
Barrel barrel = GameObject.Instantiate(prototype) as Barrel;
barrels_.Add(barrel);
Debug.Log("Barrel newly created: " + barrel.ToString()); // null Formation
Debug.Log("Barrel prototype: " + prototype.ToString()); // not null Formation
}
The ToString tells me that the numProjectiles part of the Barrel was cloned correctly, but the Formation part is null in the clones. The Formation is not null in the prototype. My formation class looks like this.
public interface Formation
{
List<Quaternion> CalculateDirections(int numMembers);
}
[System.Serializable]
public class FanFormation : ScriptableObject, Formation
{
[SerializeField] public Transform transform_;
[SerializeField] public float angle_;
List<Quaternion> Formation.CalculateDirections(int numMembers)
{
...
}
...
}
Originally this class didn't inherit from ScriptableObject and didn't have public fields but I added them in my attempts to get this problem solved.
Why do the clones have null Formations when the object they're cloning from does not?
Edit: I discovered that it doesn't work when I specify the interface but it does work when I specify the class:
[SerializeField] Formation projectileFormation1_; // doesn't work
[SerializeField] FanFormation projectileFormation2_; // works
Still not sure how to fix it.
Your answer