# How to store references to scene objects in prefabs?

i am instantiating a few game objects in my scene using prefabs and i would like to attach a script to some of them to do something at a certain time but for that i need to reference to scene objects and its not allowing me. anyone know why? and whats the workaround?

i can pull those prefabs into my scene and use them as game objects but that would destroy the idea of prefabs isn't?

• I don't think I have the facility to post an answer to the quality level I would like, but you would not save a link to the scene objects in the prefab. You would use script to create the links dynamically. There are several ways to do this; all very simple, and convenient in different ways. You could also set this up to work when you hit play and run the script in-game or at the instant where you drop a copy of the prefab into your scene. I will post an answer, when I can, if others are not satisfactory. – Gnemlock Jan 3 '17 at 0:31

In the editor, prefabs can only reference other prefabs. Prefabs are supposed to be shared between scenes. That means they can't rely on the object being present in every scene.

If you need them to reference something in the scene, you need to get that reference at runtime. There are several ways to do that.

• Attach the reference to the object which instantiates the prefabs and have the spawning script set it on every object it instantiates:

GameObject bullet = Instantiate(bulletPrefab, transform.position, transform.rotation); bullet.GetComponent<BulletScript>().shooter = gameObject;

• Search for the game object in the Start method of your prefab.

• Store the reference to the object in a global variable (a public static variable of an appropriate class) so all the instantiated prefabs can access it.
• I believe finding with tag is fast, but I could be mistaken – Evorlor Jan 3 '17 at 13:28
• @Evorlor That depends on how many objects you have with the same tag. – Philipp Jan 3 '17 at 14:25
• It tends to be faster because you have less objects in the pool to search through. However, it will be as slow as Find if most of your objects have the tag. – Leonardo Raele Oct 17 '18 at 20:55

Unity does not allow you to reference scene objects to prefabs. It allows any other reference. It's because prefabs have a template-like nature and thus are unaware of the scene.

There are many workarounds for this case, most of them involve a static variable.

1. Assign a Tag to your scene object and then find it by searching through objects

var all = FindObjectsOfType<GameObject>();
foreach ( var item in all ) {
if (item.Tag.CompareTo("MyTag") == 0)
return item;
}
//or
var item = GameObject.FindWithTag("MyTag");

2. Add a script to your scene object and store the reference there

internal GameObject MyRef;
void Awake() { MyRef = this; }

3. Add an empty game object (reference manager) in your scene with a script to store all references

//assign these in inspector
public GameObject MyRef1;
public GameObject MyRef2;


## Note that

in case 2,3 you may use static variables or singleton pattern.

e.g.

case 2:

public class SceneObject1 : MonoBehaviour
{
public static GameObject MyRef { get; private set; }
void Awake() { MyRef = this; }
}


case 3:

public class RefManager : MonoBehaviour
{
static RefManager instance;
void Awake() { instance = this; }

public GameObject MyRef1;

//use this in prefab script
public static GameObject MyRef1 { get { return instance.MyRef1; } }
}

//in prefab
var inPrefabVariable = RefManager.MyRef1;


The prefab acts as a template from which you can create new object instances in the scene.

The purpose of prefabs is to be instantiated into the scene.

When in the Project tab, it acts as a "regular file" and must be independant from any object in any scene (but it can have references from other prefabs also in the Project tab) so as to be reusable in any scene.

The advantage of prefabs is, when you edit the values of one of the and hit "Apply", all the instances in the other scenes will have the new values. (Except if you have already overriden the values of the instance)

You have to instantiate the prefab into your scene so as to be able to link objects of the scene into your prefab. The instantiation and creation of the links to the prefab instance can be done using the inspector, or, at runtime, using code.

• yes but whats the work around? – Mohit Saxena Jan 3 '17 at 6:56
• The only "workaround" is to instantiate the prefab and create the links by hand (either through code or with drag & drop) – Hellium Jan 3 '17 at 7:09
• This seems to miss the question entirely; the user is not asking us to explain prefabs. They are asking why it can't store game object references. In actual fact, it can. While the user is assumingly trying to create a hard coded reference, we can still make references and set them up dynamically. You seem to mention this in a comment, but I'm not sure how useful this answer would be to users not knowing how to set that up. – Gnemlock Jan 3 '17 at 8:28
• By explaining what a prefab is, the asker can see that it's not possible to store a reference of a scene's object into prefab. You can add references to an instance of a prefab, but not a prefab itself. – Hellium Jan 3 '17 at 8:35

One workaround is to note the instanceID of any scene object references, they are merely ints, and will save to the prefab just fine. In order to restore missing object references in new instances of the prefab, write a script that searches for GameObjects whose instanceID match with the ones we stored in the prefab. To get a full list of all available GameObjects in all open scenes, you can use eg GameObject.FindObjectsOfType(typeof(GameObject));

I'm not suggesting this is a robust solution, or a good idea, just that it is possible to deal with this kind of scenario.