Cannot assign gameObject to Transform variable in Insepector on a prefab.

I have a prefab for a ball with a mesh, rigid body, collider and two scripts.
One of these scripts has public variables for a Rigidbody, and for a Transform, which is used as the position at which to instantiate the ball when it is needed.

If I try to drag an empty gameObject whose transform data I want to use as the position to spawn the ball at to this variable in the inspector, it doesn’t allow me to do so. I get that circle cancel cursor.

However, if I create a gameObject out of the prefab in the scene, and do the same, then it works.

What gives?

1 Answer

1

This is by design. A prefab can be considered a blueprint for a game object, so at blueprint time, it cannot link to external game objects. You can link to children in the prefab. The typical solution is to use GameObject.Find() or GameObject.FindWithTag() to first find the game object in the scene, then you can get components as needed.