Editor script: Variable created on gameObject in editor set to missing on runtime
I have created a script for creating roads in a grid pattern. I have three scripts which I've pasted below.
RoadLogic is on every road game object giving it the 4 game object variables north, east, south, west for storing the road pieces that's connected around it.
ObjectBuilderScript is the script for creating the actual roads. Every road piece have 4 connector game objects as children which has the ObjectBuilderScript on them, the connector game objects are spheres placed in each cardinal direction.
ObjectBuilderEditor is just a editor script for creating a button in the default inspector.
So what's supposed to happen is that I select one of the connector spheres. Choose a prefab and press build object. The prefab should be placed at spawnPoint and be parented to an empty object called "roads". And this works on some level. The problem occurs when going into runtime, it seems like all the roads lose their north, south, west, east variable connections. Instead they display as missing.
Any help is appreciated, thanks!
Update It seems like the game objects remember which game object created them. It seems game objects only lose connection to objects they create. So for example an intersection which created 4 roads going from it lost all it's connections, but the roads created from the intersection remember that they were created from the intersection, but not which roads they created in front of them.
roadLogic:
using UnityEngine;
using System.Collections;
public class roadLogic : MonoBehaviour {
public GameObject north, east, south, west;
}
ObjectBuilderScript:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class ObjectBuilderScript : MonoBehaviour {
public Object m_obj;
Vector3 m_spawnPoint;
GameObject m_newNeighbour;
roadLogic m_parentRoadLogic;
roadLogic m_neighbourRoadLogic;
GameObject m_parent;
public void BuildObject()
{
switch (gameObject.name)
{
case "north":
m_spawnPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z + 6);
m_newNeighbour = PrefabUtility.InstantiatePrefab(m_obj) as GameObject;
m_newNeighbour.transform.position = m_spawnPoint;
m_newNeighbour.transform.parent = GameObject.Find("roads").transform;
m_neighbourRoadLogic = m_newNeighbour.GetComponent<roadLogic>();
m_parentRoadLogic = transform.parent.GetComponent<roadLogic>();
m_parentRoadLogic.north = m_newNeighbour;
m_neighbourRoadLogic.south = transform.parent.gameObject;
break;
case "east":
m_spawnPoint = new Vector3(transform.position.x + 6, transform.position.y, transform.position.z);
m_newNeighbour = PrefabUtility.InstantiatePrefab(m_obj) as GameObject;
m_newNeighbour.transform.position = m_spawnPoint;
m_newNeighbour.transform.parent = GameObject.Find("roads").transform;
m_neighbourRoadLogic = m_newNeighbour.GetComponent<roadLogic>();
m_parentRoadLogic = transform.parent.GetComponent<roadLogic>();
m_parentRoadLogic.east = m_newNeighbour;
m_neighbourRoadLogic.west = transform.parent.gameObject;
break;
case "south":
m_spawnPoint = new Vector3(transform.position.x, transform.position.y, transform.position.z - 6);
m_newNeighbour = PrefabUtility.InstantiatePrefab(m_obj) as GameObject;
m_newNeighbour.transform.position = m_spawnPoint;
m_newNeighbour.transform.parent = GameObject.Find("roads").transform;
m_neighbourRoadLogic = m_newNeighbour.GetComponent<roadLogic>();
m_parentRoadLogic = transform.parent.GetComponent<roadLogic>();
m_parentRoadLogic.south = m_newNeighbour;
m_neighbourRoadLogic.north = transform.parent.gameObject;
break;
case "west":
m_spawnPoint = new Vector3(transform.position.x - 6, transform.position.y, transform.position.z);
m_newNeighbour = PrefabUtility.InstantiatePrefab(m_obj) as GameObject;
m_newNeighbour.transform.position = m_spawnPoint;
m_newNeighbour.transform.parent = GameObject.Find("roads").transform;
m_neighbourRoadLogic = m_newNeighbour.GetComponent<roadLogic>();
m_parentRoadLogic = transform.parent.GetComponent<roadLogic>();
m_parentRoadLogic.west = m_newNeighbour;
m_neighbourRoadLogic.east = transform.parent.gameObject;
break;
default:
break;
}
}
}
ObjectBuilderEditor:
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(ObjectBuilderScript))]
public class ObjectBuilderEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
ObjectBuilderScript myScript = (ObjectBuilderScript)target;
if (GUILayout.Button("Build Object"))
{
myScript.BuildObject();
}
}
}
Sorry for the somewhat messy ObjectBuilderScript, I will put the functionality in a function ins$$anonymous$$d of rewriting it 4 times.