Modified Save/Load methods not saving properly
I'll start by saying I posted about this on the forums, but maybe its better to post here instead
I found a tutorial on making a hexgrid style game that I'm trying to make into a HOMM V clone. I'm modifying the code to allow more than just one prefab for the unit object. However when it comes time to load the map it always loads the first unitPrefab object in the array. Which I tracked down to be a bug that is because it isn't loading the variable that stores the heroID. Using BinaryWriter it should be storing the int value so when the unit.Load method is called it will check the value and instantiate the right prefab. http://catlikecoding.com/unity/tutorials/hex-map/part-18/ this is the site of the tutorial. In the tutorial the author has it so the unit that already exists on the grid will load itself based on its prefab:
public static void Load (BinaryReader reader, HexGrid grid) {
HexCoordinates coordinates = HexCoordinates.Load(reader);
float orientation = reader.ReadSingle();
grid.AddUnit(
Instantiate(unitPrefab), grid.GetCell(coordinates), orientation
);
}
// What I changed it to:
public static void Load (BinaryReader reader, HexGrid grid) {
HexCoordinates coordinates = HexCoordinates.Load(reader);
float orientation = reader.ReadSingle();
int unitID = reader.ReadInt32();
if(unitID == 0)
grid.AddUnit(
Instantiate(unitPrefab[0]), grid.GetCell(coordinates), orientation
);
if(unitID == 1)
grid.AddUnit(
Instantiate(unitPrefab[1]), grid.GetCell(coordinates), orientation
);
...
}
My modification to this was just including an int unitID = reader.ReadInt32(); after the orientation load (which I have writer.Write(unitID) in the save method). Then I would wrap the grid.AddUnit() call in an if statements that checks its unitID then where it uses unitPrefab I have unitPrefab[index] instead for each unitID possibility (which is 6 currently).
I'm not sure if I have the definition of the unitID setup correctly. From examples I've seen around and some ways the author does other values I set it up like so:
public int UnitID {
get { return unitID;
}
set { unitID = value;
}
}
int unitID;
I must be doing something wrong somewhere.. Just not sure where it is happening.
Answer by L33T_Rakshasa · Dec 08, 2017 at 03:20 PM
So after some Debug.Log trial and error I figured it out. At least why the value I attempted to save didn't persist upon load. It was because I was trying to set the value via the inspector instead of through a method. I didn't know using the inspector to set the value didn't work.. At least now I know!
Now if I could just figure out how to get an array of prefab objects to instantiate based on the index passed on load.
Your answer
Follow this Question
Related Questions
Debugging with drawing a radius 1 Answer
camera can't focus on player 0 Answers
Wrapping Debug.Assert and skip wrap method in the stack trace 2 Answers
GUI not updating after scene change on IAP button click? 1 Answer
C# float is 0 in debug 2 Answers