- Home /
Instantiate moving prefab
So, I am not even quite sure of how to inquire about this, however, I have a simple test game built where squares will "swarm" a player object.
My initial thoughts (which I am still using) was to build a prefab with a movement script attached, and then build a spawner object that will spawn the moving prefab clone.
As might be obvious this poses a problem because I can only build a prefab with objects already present within the game, meaning that my prefab is already moving when I start the game.
Obviously this functionality is unwanted. So I have paused to ask how I SHOULD be approaching this situation.
My goal is to spawn squares at a determinable rate and speed. I know I COULD do this in one script, but I was thinking it would have been cleaner and more organized to have the movement and spawning functionality separate from each other.
One solution I was thinking of was to only 'move' the prefab IF it is in or out of a certain area -- ie, IF prefab is in game area, move as intended, otherwise don't. This would allow be to set up any number of different styled moving objects.
But again I am unsure of what the right course of action would be. Or, at the very least, the most advisable course.
Thank you sincerely for any help. :) -- also, I apologize if my intentions aren't very clear, don't hesitate to ask for clarification! :)
because I can only build a prefab with objects already present within the game, meaning that my prefab is already moving when I start the game.
A true prefab doesn't exist in the game until it is Instantiated:
https://docs.unity3d.com/Documentation/$$anonymous$$anual/Prefabs.html
Hmm, if I am not mistaken, that only reiterates what I stated. To create a prefab, it has to exist within the game / hierarchy first.
I should mention that the prefab I was attempting to make was sprite based, so, not sure if that matters.
actually a prefab does not exist in any way other that being in your project window. it should only be in your hierarchy if you need it to be in your game at launch. try deleting your prefab (make sure it really is a prefab by making sure it's name and icon is blue) and then try instantiating it by using Instantiate(yourPrefabName, position, rotation);
Is / did.
If I create an empty prefab, I can't add anything that isn't within the hierarchy -- fair enough. But the only way I can add a sprite to the hierarchy also adds it to the game window, which, for all intents and purposes, is the game itself. Thus, adding movement functionality (as in, go here, do that) to an object already in the game window, means, when I hit play, it will start moving.
So, while, yes, the prefab itself doesn't exist yet, within the context of the game, the object its based off of does, so if I were to, lets say, have a function that destroys said object if it hits the player, it will of course destroy the prefab also, no?
however the object in the hierarchy you can just delete if you don't want it to be in the game from the start.
Ohhh. I apologize. I had no clue I could delete the original after making the prefab.
Thank you VERY much. :) I really appreciate you taking the time to explain that to me, also, I apologize for being a bit dense. :P
Too bad I can't mark your comment as the answer... :(
Answer by xSmurfKasketter · Mar 23, 2014 at 10:01 AM
i'm don't think that you are totally aware of what a prefab is or how you create it. Let's say that you have an object called enemy that you want to make a prefab. you drag it out into the hierarchy and attach scripts and whatever components you want on it. you then drag it back down into your projects window and that makes it a prefab. however the object in the hierarchy you can just delete if you don't want it to be in the game from the start. And afterwards just use the Instantiate() function to create your prefab again. I'm not sure what you mean will it destroy your prefab. if you instantiate a prefab twice and destroys one of them, the other one won't be affected and you can instantiate one again. They are independent of one another.