I’ve been attempting to create an enemy that spawns missiles that track the player. I’ve created the missile prefab but I’ve noticed that I cannot attach the player to the empty GameObject variable. The way I’ve tried to do this is by clicking on the prefab in the assets menu and dragging the Player GameObject from the hierarchy to the empty value slot to no avail. Is there a way I can permanently assign the target to the prefab? Here is the exact error when I start the game with it unassigned:
MissingReferenceException: The variable target of MissileScript doesn’t exist anymore.
You probably need to reassign the target variable of the ‘MissileScript’ script in the inspector.
You can’t assign gameobjects from your scene onto a prefab. There are ways to assign it when you instantiate. First, you could find the Player using something like gameobject.find.
Another way is your have another script in your scene that has a reference to player and you can then pull that value out of there.
Depending on your missiles, you could also use object pooling where the missiles exist in the scene in an off state and they would have a reference to the player. And you use a missile as needed, but this may depend on the variety of missiles you have and how many you plan to have on the screen.
I will appreciate anyone who can explain to me in a logical way why we cannot assign prefabricated gameobjects in an era where we can go to space… I’m in a complicated situation where I need this. Calling a game object via Prefab, Gameobject.find or by reference won’t work. I need to assign directly. And this is not possible. Because it violates the laws of physics? Why can’t it be done? Sometimes I have a hard time understanding things… I never understood that in the 21st century and in something like software there are still ridiculous limits. Anyway.
@vbarisbuyukturk I would create a new post, if you’re having an issue related to this. That post was made back in 2016, and Unity has evolved a thousand times over. There should be no issues with putting a prefab into anything, as long as your grabbing it from Project, and not Hierarchy.
And GameObject.Find() works perfectly fine, but is argued among seasoned coders, to ever using it in the first place. There is always another way.
Thank you for your answer. I will create a new topic There are dozens of fields in the game where I use the gameobject.find() method. But it will be unfair to the player to tire the processor for a simple prefab in a more complex mechanic. Whereas, I’m trying to understand why it’s so imposible for prefab to just drag and drop a game object. I think 3 minutes will be enough to add this feature in Unity. I’m pretty sure a library or service won’t conflict or be a logical error. Still, I wish a more experienced programmer would make a statement as clear to me as “that’s not possible, we can’t see God because it is a spiritual being”. :) I really need this, I’m trying to understand.
@vbarisbuyukturk I read deeper into the question originally posted.
The reason you can’t take something that exists in a scene, and save that as a declaration within a prefab, is because it won’t exist in every scene. So it’s more of a fall back to basically say in a nutshell “this action makes no sense”.
To put more simply, say you load a bonus round, and it’s no longer “player” you control, or is within said scene. You can’t instantiate a prefab with a “non-existent” or “unsaved” reference call. Talk about errors! lol.
But trying to even take the player prefab, from project, won’t be able to serialize(get) the appropriate instance(particular player), to know which player or component you want. So even that wouldn’t make any sense, unless you were just using it for a check of type(which I wouldn’t recommend).
So one way, would be to make player(instance) a singleton. Then any calls or checks against that would always reference the correct script(instance). You could also set a tag on player, and say FindObjectWithTag(). There are multiple ways of doing it.