Prefab problem, wont accept gameObject in var

Ok this is something I haven’t run into yet. I tried looking on here for similar situations to find nothing. Here is basically what is happening. I have an object called “Enemy” and has its own script called “EnemyScript.” In the script I have a variable called “player” that is GameObject type, so the enemy can look for the player object. So in the inspector I dragged and dropped the player object into it, and it works completely fine.

So I am trying to create a prefab for the enemy (I think this will solve my other problem on enemy spawning, where when I kill the “Enemy” object it stops spawning them). I create the prefab and drag and drop the “Enemy” object into the prefab but it deletes the player GameObject out of the var in the inspector for it, and it wont let me place it back in.

Hopefully that is understandable, if not I can try and make it ore clear.

You cannot have scene objects in prefab members.

Once you instantiate them, then you can assign them through code. Or, if you have added the prefab to your scene, you can assign it as you did before.

You could either

A - Use another method to select the player GameObject

GameObject player;

Start() {
    player = GameObject.Find("Player"); // Name of the player object
}

B - Create a prefab for the playerObject

This method allows you to add the player to the enemy prefabs

If the object is not a prefab, the editor will not know if you will put it into every single scene, as such it doesn’t allow you to add them to prefabs, which can be added at any time into the scene (including during runtime). Because of that it simply doesn’t allow you to do that.