- Home /
Are there restrictions on assigning GameObjects to Prefab script variables?
I ran into what seems to be a restriction on how I can assign existing GameObjects to public script variables. I didn't see anything about it in the documentation, it should work, but, doesn't...
I have two Prefab asset/models, called pf-Box and pf-Tube. I drag pf-Box onto my Scene, so it's now instantiated at development time (is that the opposite of runtime?), as aBox.
pf-Tube will be instantiated at runtime, and I want it to have a reference to aBox, so I create a script, called s-Tube, with a public variable, tmpObject, and add the script as a Component to the Prefab pf-Tube.
Then, in the Inspector window, I select the triangle dropdown box for tmpObject, and try to assign aBox to it - but it won't assign.
Looking at the dropdown box, it separates objects into two categories, Assets and Scene. I can assign things in Assets to my script variable - but I can not assign objects already existing on the Scene to my variable.
Is that correct? Are there restrictions on what objects can be assigned to a (not-yet-existing) script variable? Or is there some deep, dark configuration option that I don't have set right to allow it? :)
Note - I know there are other ways to reference existing GameObjects. But using a public script variable is a lot simpler, and seems like it should work.
John C>
"For all your days, prepare, and meet them ever alike;
When you are the anvil, bear - when the hammer, strike."
Answer by Eric5h5 · Feb 08, 2010 at 02:57 AM
You can't assign scene objects to prefabs, because prefabs must be universal and scene objects only exist in a single scene. Imagine being able to assign a scene object to a variable on a prefab, and then dragging the prefab into a different scene where the first scene object doesn't exist--it can't work.
Ah, that makes sense. While learning Unity, I hadn't really paid attention to the Scene versus project aspect, but now I get it. Thanks.
Answer by Jaap Kreijkamp · Feb 08, 2010 at 07:16 AM
Indeed, what Eric5h5 is correct. There's one exception however. You can reference to gameobjects that are either children of the prefab or the prefab itself. Example.
Given the following Scene.
+ Player
|
+ Enemy
|
+ Gun
and Enemy has a script:
var player : Transform;
var gun : Transform;
When you set player
to Player
and gun
to Gun
and make a prefab of Enemy
. The value of gun
will be kept, but player
, referring to a gameobject not part of the prefab, will set to null if you instantiate the prefab.
Answer by MakoBec · Oct 10, 2014 at 09:32 AM
you can place the prefab to the scene then assign the gameobject to the inspector of the placed prefab.
Your answer
![](https://koobas.hobune.stream/wayback/20220612054258im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Access variable from script on other gameobject. 1 Answer
Prefab problem, wont accept gameObject in var 3 Answers
How can I modify a variable on an instance of a prefab after I created it? 2 Answers
Prefab (Project Folder) to a GameObject (on the Scene) 2 Answers
How can i access in a prefab a variable from a gameObject? 1 Answer