- Home /
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.
Answer by Democre · Dec 01, 2012 at 03:55 AM
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.
Im not sure what you mean, or how exactly how to do that. Would it help if I posted my code(s) and script(s) for my enemy stuff. I have a script for the enemy(used to make the enemy look and chase the player) and one for spawning the enemies around the scene.
Heck maybe by showing that info, I might not even need to create a prefab as ultimately Im trying to make it that when I kill the original enemy, it wont stop spawning the others. I get some kind of message that its trying to call a transform that has been destroyed.
Prefabs could be instantiated anywhere by code, including a different scene that does not contain a GameObject previously assigned: NullRefs.
Answer by AdamtheNoob · Dec 07, 2016 at 05:43 AM
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.
Your answer
Follow this Question
Related Questions
How do I assign a prefab reference to a private variable? 1 Answer
Setting a game object from a list to be the active character that gets instantiated 1 Answer
What is the type of my prefab? 2 Answers
Set Rigidbody variable to Prefab using code 1 Answer
Disable/Enable game object by clicking instantiated prefab during runtime 0 Answers