- Home /
How to reference a prefab in a class array from a json
I am populating a JSON with an array of classes. Each class contains another array of classes that store variables about an encounter-able event. One of the variables in the class is a public gameobject which contains a prefab with a 3d model component. If I reference the prefab in the JSON is stores it as a volatile instanceID that changes every time the editor is re-launched. The system will work perfectly until the instanceID changes. I'm trying to set it up such that when I add a new event in the future, I don't have to change things in two places. I was thinking of making an independent public gameobject array in an eventmanager script. However that will mean making a new event will require increasing the class array, entering the data. Then adding the prefab to a separate game object array which doesn't sound like much but when there are hundreds of events it will become tedious. Here are the classes:
[System.Serializable]
public class ExternalEvents
{
public ExternalEvent[] m_ExternalEvents;
}
[System.Serializable]
public class ExternalEvent
{
public GameObject m_EventObject;
public string m_EventName;
public string m_Description;
public int m_ExternalEventCode;
public int m_MinProbabilityRange = 0;
public int m_MaxProbabilityRange = 0;
public bool m_IsUnique;
}
Here is the method that instantiates the prefab when an event is encountered.
void CreateRandomExternalEvent()
{
int i = Random.Range(0, 1000);
m_ExternalEventOffset = new Vector3(Random.Range(25, 40), 0, 0);
for (int j = 0; j < m_ExternalEvents.Length; j++)
{
if(i >= m_ExternalEvents[j].m_MinProbabilityRange && i <= m_ExternalEvents[j].m_MaxProbabilityRange)
{
Instantiate(m_ExternalEvents[j].m_EventObject, m_MainShip.transform.position + m_ExternalEventOffset, Quaternion.identity, m_LevelObjects.transform);
m_EventsActive = 1;
break;
}
}
}
So ultimately the questions is, what is an easy and elegant way I can reference the prefab that won't break every time the editor is re-launched. I understand this isn't so much a cut and dry technical question it's more of a request for a suggestion but I don't know anyone who knows a lot about unity so I thought I would try the official forums.
Answer by mchts · Mar 23, 2019 at 09:57 AM
Move your prefabs in a Resources folder under Assets and name them uniquely. Instead of using public GameObject m_EventObject;
store the name of the prefab like public string m_EventObject;
. And when it comes to instantiate do like following:
Instantiate(Resources.Load(m_ExternalEvents[j].m_EventObject, typeof(GameObject)), m_MainShip.transform.position + m_ExternalEventOffset, Quaternion.identity, m_LevelObjects.transform);
This line above will look for any Resources folder under Assets. If your prefabs are in a specific path you must add that as a prefix to your m_ExternalEvents[j].m_EventObject. For e.g. "MyAllPrefabs/ExternalEvents/"+m_ExternalEvents[j].m_EventObject
. Hope this helps
Your answer
Follow this Question
Related Questions
Problem instantiating a prefab, and storing a reference to it. 1 Answer
How to instantiate an object on multiple positions? 0 Answers
Intantiated objects being created but being invisible...[PICTURES] 1 Answer
why is it slow to instantiate 0 Answers
instantiating prefab does not rotate child objects correctly? 1 Answer