Question by
Janihi · Jul 24, 2021 at 07:00 PM ·
c#unity 5scripting problem
How can I load/save a Prefab refference?
Iam trying to add a loading/saving system to my game right now. Most things work with numbers so thats easy to save and load. What iam struggeling with right now is how to save or load a Prefab, so what type of GameObject is supposed to be instantiated in the load. I guess using an Object tag to refference it dosnt really work but i havnt been able to find a working method for it.
Code:
public void Save(BinaryWriter writer)
{
writer.Write(terrainTypeIndex);
writer.Write(elevation);
if (hasChild == true)
{
** ObjectTag thisObject = gameObject.GetComponentInChildren<ObjectTag>(); **
float positionX = thisObject.transform.position[0];
float positionY = thisObject.transform.position[1];
float positionZ = thisObject.transform.position[2];
float rotationX = thisObject.transform.rotation[0];
float rotationY = thisObject.transform.rotation[1];
float rotationZ = thisObject.transform.rotation[2];
** string objectTag = thisObject.tag; **
writer.Write(hasChild);
writer.Write(positionX);
writer.Write(positionY);
writer.Write(positionZ);
writer.Write(rotationX);
writer.Write(rotationY);
writer.Write(rotationZ);
writer.Write(objectTag);
}
}
public void Load(BinaryReader reader)
{
terrainTypeIndex = reader.ReadInt32();
elevation = reader.ReadInt32();
hasChild = reader.ReadBoolean();
if(hasChild == true)
{
** string objectTag = reader.ReadString(); **
** GameObject thisObject = Instantiate<ObjectTag>(GameObject.FindGameObjectsWithTag(objectTag)); **
Vector3 objectPosition = new Vector3(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
thisObject.transform.position = objectPosition;
Quaternion objectRotation = new Quaternion(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), 0);
thisObject.transform.rotation = objectRotation;
}
Comment
Your answer
Follow this Question
Related Questions
Problem with Score/Highscore script 0 Answers
Collision Detection Not Working (Unity 5.2.3) 5 Answers
Child class cannot access property in method? 1 Answer