- Home /
Help how to reference prefab for instantiating between loads.
Please help, I just can't figure this out :(
I have a script which generates random a level by selecting individual sections from a list of GameObject prefabs (startingPrefabs). I want to let the player save a level layout they like, so each time a GameObject is selected from the list, I add that object to a new list called savedPrefabs (I am not adding the instantiated object to the list, but basically saying: savedPrefabs[index].Add(startingPrefabs[index]). (see below) I then save that list to json. This works fine... until I stop the game and reload. As soon as I reload the GameObject is no longer recognized and I get a reference null when I try to rebuild the level.
In my json file my GameObject list is a list of these:
{"instanceID":15058}
I assume the instance ID is a temporary ID which is assigned each time the game loads. So in this case how do I reference my actual prefabs and not their instance ID's?!
Originally my starting list was not a GameObject list, but a custom class list and I was instantiating as my custom class, and after reading a few other posts here I thought that was my problem. So I changed it to a GameObject list, then instantiate the custom class component of the GameObject. But this STILL ISNT WORKING :(
Here is a simplified version of the code.
[SerializeField] List<GameObject> startingTilePrefabs = new List<GameObject>();
//set my prefabs in inspector
List<GameObject> storedLayoutPrefabs = new List<GameObject> ();
void placeSections(){
int ran = Random.Range(0, startingTilePrefabs.Count);
//choosing random section
tileTiles convertedCurrentTile = startingTilePrefabs[ran].GetComponent<tileTiles>();
tileTiles currentTile = Instantiate(convertedCurrentTile) as tileTiles;
//spawning random section by instantiating as my custom class "tileTiles"
GameObject saveTile = startingTilePrefabs[ran];
storedLayoutPrefabs.Add(saveTile);
//saving random section to list
}
//and then after passing that list to json I reload using...
void reloadLevel(){
foreach (GameObject prefab in storedLayoutPrefabs)
{
tileTiles convertedTile = prefab.GetComponent<tileTiles>();
//the reload breaks here with null reference
tileTiles placingTile = Instantiate(convertedTile) as tileTiles;
}
Obviously there is a lot more to the code than this, but this essence of the system. It works perfectly until I reload when I guess the {"instanceID":15058} has no meaning.
Please please please, how can I pass the identities of the prefabs themselves into my json list? I've searched and searched for this one and am getting nowhere. I thought by changing my starting lists to game objects and collecting that info to a new list would fix the problem, but is hasn't, and I really don't know where to go now.
If the prefabs have unique names, just save their names and then reload them by their name using Resources.Load. If not, give your prefabs a unique ID yourself, for example by using GUID.
Thank Cherno. The prefabs do have unique names, I suppose I could grab the name as each one is chosen from the list, save that to a string list and use that to load resources? Sounds plausible doesn't it? It would still be nice to get my current structure working, without the need to abandon the json. I am not sure about the GUID thing, I think that is the ID I am already getting isn't it? But it isn't working from that... beyond that I don't rly understand how you can manually set GUID. There is a function to collect GUIDs from selected objects but I don't see any way you can set it yourself, https://docs.unity3d.com/ScriptReference/Selection-assetGUIDs.html
Your answer
Follow this Question
Related Questions
Referencing gameObject from script after Instantiate 0 Answers
Spawn Prefabs 1 Answer
How to instantiate a prefab with a script attached? 2 Answers