- Home /
Instantiating a prefab via PrefabUtility.InstantiatePrefab causes prefab to lose all references on it's scripts.
I've been creating a in-editor 3D tilemap tool with autotiling features for the past few days and I've come across quite a big problem. All my tiles are prefabs, and have a script that controls autotiling.
To update a tile, first I create a mask based on it's neighbours and then get the corrosponding 'rule' from a dictionary. The dictionary contains the new tile and some extra info. The problem occurs however when I instantiate the new tile. Here's the code for that
//Get the actual prefab from the assets (otherwise we'll be trying to instantiate the wrong thing)
GameObject basePrefab = PrefabUtility.GetCorrespondingObjectFromOriginalSource(newTile);
GameObject tile = PrefabUtility.InstantiatePrefab(basePrefab) as GameObject;
When instantiated, I noticed that the script attached to the prefab loses all references to objects. Here's a before and after
https://imgur.com/a/1lIKFvH (top is before prefab is instantiated, bottom is after)
Why is this happening? WallSide DOES refer to an actual prefab by the way. This is prove by the fact selecting the non-null reference highlights the prefab in the project files. Also the fact the scene is empty and thus it cannot be referring to any kind of scene object.
Your answer
Follow this Question
Related Questions
Particle system prefab not instantiated 1 Answer
Is there a recommended workflow to pre-process prefabs before making a build? 0 Answers
Referencing prefab in code without using editor or Resources.Load? 0 Answers
PrefabUtility.CreatePrefab fails if prefab already exists 1 Answer
Script on nested prefab Loses referance to parent prefab when instantiated 0 Answers