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;
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.
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.
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.
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?
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.