• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gulliwog · May 27, 2018 at 12:31 PM · gameobjectinstantiateprefabnullreferenceexceptionjson

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cherno · May 27, 2018 at 01:11 PM 0
Share

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.

avatar image gulliwog Cherno · May 27, 2018 at 03:24 PM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

153 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Referencing gameObject from script after Instantiate 0 Answers

How to instantiate prefab,Instantiating prefab is not wokring in this script 0 Answers

Check if object is destroyed on level load, if so instantiate prefab? 1 Answer

Prefab Losing Target Transform 0 Answers

How to instantiate within GameObject 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges