I am at a loss for words here. I have written a game save/load system that converts the data I want to JSON and then back again. It works except I have a bug that I am pretty sure I didn’t used to get and I haven’t changed the code.
Basically when serialising the type of a component on an object is saved, and then also any relevant fields are saved with it.
When loading, it instantiates the relavent prefab then goes through each of the components in the save file. If the component already exists on the prefab, it just copies over the field values. If the component doesn’t exist, it first adds it before then copying over the fields.
This works on all my prefabs except one - my camera.
It instantiates the prefab, but then when it gets to the CameraController component, it doesn’t seem to be able to find it and so adds a new one. Leaving my camera with two camera controller scripts (one of which then gets the fields copied over, and the other one left with the default - all zeros)
Here is the code that does this little bit:
foreach (string componentName in componentNames)
{
//Get the component
Component c = obj.GetComponent(componentName);
Debug.Log(obj.name + "->" + componentName + ": " + c);
//If the component doesn't exist (i.e. not one of the components on the prefab) then add it
if (c == null)
c = obj.AddComponent(Type.GetType(componentName));
//Get the component properties
JObject componentProperties = (JObject)JsonConvert.DeserializeObject(componentsGroup.GetValue(componentName).ToString(), settings);
//Load the component data into the component
JObject data = (JObject)componentsGroup.GetValue(componentName);
JsonUtility.FromJsonOverwrite(data.ToString(), c);
//Add the instance id of the component to the lookup
int instanceId = (int)componentProperties.GetValue("instanceID");
if (!instanceIdLookup.ContainsKey(instanceId))
instanceIdLookup.Add(instanceId, c);
//Set compoonent enable
bool enabled = (bool)componentProperties.GetValue("enabled");
((MonoBehaviour)c).enabled = enabled;
}
So that debug log for everything except the camera is as expected and print the component. For the camera I get
“Main Camera->CameraController:”
Nothing, it can’t find it.
So then I thought, okay sanity check. I’ll add a loop that prints all the components on the “c” object. And sure enough the camera controller is there. Spelt exactly the same (as it should be, the script generates the names directly from the component when saving so can’t really get a typo).
But I thought, maybe somehow some invisible characters I creeping in and messing it (no idea how but worth checking).
So I compared the length of the componentName string, with the name of the component that was found (using the same method as the save file → component.GetType().ToString()). They are the same length.
I even did a loop, checking each individual character of both strings - they match.
So why on earth is GetComponent not finding it?
Any ideas because I’m all out?
Can add any other info if needed.
