Cannot attach GameObject to a prefab

How can I attach GameObject placed on the scene to a prefab (prefab has a script and I want to set object placed on the scene as default value of this script).
There is simple declaration in script:
var obj : Transform;

Does anybody is it possible at all?

1 Like

In your project view, hit “create” and then “prefab”. this will get you an empty prefab. You now have to drag your game object into this empty prefab. And then, you can drag your prefab into the variable slot on the script.

2 Likes

That is not the point.
I want to have in the script reference to exact copy of prefab which is placed on the scene, not to prefab which needs to instantiate. This should be its default reference.

2 Likes

You can’t do this - you can link scene objects with other scene objects or prefabs, but a prefab can only be linked with other prefabs. You might be able to use GameObject.Find to get the scene object in the prefab’s Start function, depending on what you need to do.

20 Likes

Thanks andeeee, thats the way I’ll do it.

I really love you in this moment, andeeeee

2 Likes

Thank you so much andeeeeeee
Thank you

OMG you are my savior !!!

Thaks

Can you please explain me how to exactly use that function
 Because i am a nooby soo


https://docs.unity3d.com/ScriptReference/GameObject.Find.html
You may want to call it in the start to fetch the missing references.

this is the solution I find working, thank you andeeeee

This is not a solution. Yes for simple logic games. It is called within the start function. But completely unsuccessful for a complex game. Instead of saying “You can’t do this” you can deal with the bullshit why this can’t be done in the Unity game engine in 2023.

My question is very simple: Why can’t a gameobject be added to a prefab?

2 Likes

THANK YOU SO MUCH

Thanks for answer, much apreciated, but is there a way to do it without using ‘Find’? There are several well documented problems using Find, e.g. changing a name of the game object would cause an error. In addition I can see problems with turning the game object to a prefab, e.g. if original object is modified those changes won’t take effect to the the prefab, unless you get rid of the the original game object altogether and use the prefab instead then set it up to be similar to the original game object, which all just seems like a kerfuffle anyhow for quite a trivial task and can’t think why I can’t set a gameObject to be a gameObject via the Unity interface.

Please don’t necro threads (from 2010!). Create your to ask your own questions.

Thread Closed.