- Home /
GameObjects created from prefabs - members reverting on play.
Hello, I've set up a prefab and scattered a few instances of it around a level - but changed some of the members per instance. When starting the game, all members revert back to what were set on the prefab. Has anyone encountered this before? The values are changed before the first frame of started (verified by breaking in Start).
To give an idea of the setup, there is a VectorFieldAffector component (which is on the gameobject I am making the prefab for), which contains a VectorFieldForce. VectorFieldForce contains the members - all marked with [SerializeField], and the class as [Serializable].
Thanks!
Can you show us the code for your definition of VectorFieldForce?
edit: and also the definition of the VectorFieldForce instance variable
Can do, will post it up in the morning, but from the top of my head it's basically:
[Serializable] public class VectorFieldForce { [SerializeField] public Vector3 Position;
[SerializeField] public Vector3 Force;
[SerializeField] public float AttnC;
[SerializeField] public float AttnL;
[SerializeField] public float AttnQ; }
The [SerializeField] attributes are actually redundant - public members of a [Serializable] class are all automatically serialized, provided they're of a serializable type themselves.
That looks O$$anonymous$$ though. Need the instance variable next...
Also, do you have field initialisers in your VectorFieldForce class (e.g. public float AttnL = 10.0f;) that happen to match the prefab values?
Which is as follows:
[SerializeField] public VectorFieldForce Node;
Answer by ryba · Jan 23, 2013 at 12:25 PM
You must set those values in inspector before even hitting play button. Every change while game is running is reverted to state of saved scene while you stop game.
[EDIT] Can you provide whole class code ? Are u setting those values anywhere in the code ? Are u accessing those fields in the code directly or through property / setter getter ? (you should use setters and getters even if your field is public for inspector) If you do so, then add debug log in setter, to check if setter resets your values, or if thats something else. If you are accessing fields directly, then consider refactoring.
The values are changed in the inspector without the game being started, it's when I hit start they are reverting to the values that are defined in the prefab.
Only place the values are being set in code is in the editor class (which calculates the attenuation values and sets them). I can post up more in the morning when I'm back in the office.
Ah, the SetDirty call that people have been suggesting on Twitter sounds like a good thing to check then.
Your answer
Follow this Question
Related Questions
Can a GameObject be serialized? 1 Answer
SerializeField does not work when a value set using code 0 Answers
Custom Inspector for an array of a serialized class 0 Answers
How to prevent Editor script variables resetting at runtime 2 Answers
Starting Play mode resets values for serialized class in the Inspector 0 Answers