- Home /
Instantiate with Prefabs
Hi All, I have some code which I would like to spawn a monster. Here's a snippet of my code: ## ## public var prefab : PrefabType; public var spawnPlace : GameObject; // ........ (a lot of other stuff) Instantiate(prefab, spawnPlace.transform.position, transform.rotation) ## ##
And then I get an error:
BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEditor.PrefabType, UnityEngine.Vector3, UnityEngine.Quaternion)' was found.
I'm guessing that's because I'm using a variable type PrefabType
rather than GameObject
, but in my game my enemies only move in one direction, and I've made some code,
if (transform.position.x < **A Certain Point**){
Destroy(gameObject);
}
which destroys my enemies after they get off the edge of the screen, but if I Instantiate
a GameObject
, after a while, the "spawn object" will go past the "certain point" and no more will spawn, so that's why I want to Instantiate
a Prefab.
Does anyone know how to Instantiate
a prefab? Any help would be much appreciated.
(If its not clear, I'm a beginner with Unity, and I'm trying to get my head around the Instantiate
function/command/thingy.)
I remember I saw something about Prefabs here? http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
Does the $$anonymous$$issile one use a prefab?
Sorry, clicked delete ins$$anonymous$$d of edit, so rewriting the comment (briefer this time).
Could you not just move the spawnPlace on Update() to ensure the enemies spawn in a valid location?
You said: (I think)
I'm having troubles understanding what the intended result is. How about making a Vector3 field/property called "SpawnPoint" (or create a GameObject called SpawnPoint) and on each update move that SpawnPoint (Vector3, or object) to a valid location in your game for enemies to spawn at? It might be easier to ensure your enemies are spawning in a valid location than co$$anonymous$$g up with a special type of spawn process to get around a game mechanic.
(To Ellander) Its not that I can't get the enemies to spawn in the right place, its that all my enemies move in one direction, and will dissappear when offscreen, but the GameObject
that will be clones the others will disappear too after it goes off the screen and then... I'm screwed. :D
Answer by Ellandar · Feb 23, 2013 at 11:45 AM
I see what the problem is now. Here's how to fix your problem:
The GameObject that you are copying to make your enemies needs to be a prefab.
In your Assets folder, create a new folder called "Prefabs".
Click on your GameObject in the scene that represents your enemy and drag it into the folder called "Prefabs".
Confirm that the copy of your enemy is now in the prefabs folder. (You can test that it's correct by clicking on the object in Prefabs folder and dragging it back into your scene. If it looks the same and has all the same properties as a normal enemy it worked).
Once confirmed that the prefab is there, you can now delete all enemies (even the one you were copying from).
You now have a scene with your level, but no enemies or enemy to copy.
Now there is a couple of ways to go from here, which will probably need a little more detail from yourself. If the script you linked above is a game manager that spawns your mobs, and it's attached to a GameObject in the scene then life is easy, follow these steps:
In the script, change the prefab type back to GameObject (from PrefabType)
Click on the game manager object in the scene, you should see your script attached on the right in the inspector.
You should see a field called "prefab", drag your new prefab on top of this field and drop it in.
Now when the instantiate fires, it will spawn an enemy from the prefab. Since the prefab is not in your scene, it will never disappear.
If the script isn't a game manager and attached to an object, you'll need to find the prefab using code. To do this, one quick change is required:
In your Prefabs folder, create a new folder called "Resources" and now drag your enemy from the Prefabs folder into the Resources folder.
Now, in your code where you have:
public var prefab : PrefabType;
Delete that line.
And your instantiate line should change from:
Instantiate(prefab, spawnPlace.transform.position, transform.rotation)
to
Instantiate(Resources.Load("PrefabName"), spawnPlace.transform.position, transform.rotation)
Where "PrefabName" is the exact name of what your prefab is called within the Resources folder.
Rob.
Here's a quick picture example of a prefab in action. Thought it might help.
@Eliander great work on the answer. I would suggest not putting in the Resources folder and not doing a Resources.Load...just have it somewhere in Assets. Select the object that has the script attached, then drag and drop the prefab on top of your prefab variable.
I have to kinda disagree on that it's not always possible to get prefab just like that
if prefab calls it self a prefab it won't be prefab any longer and you must call it with resources.load happened in my example
if you don't do it that way you have to create 2 objects that will call each other as prefabs witch is total waste of time and space and efficiency
@robertbu cheers. Yes, I agree and I'm not a big fan of the resources load option unless you don't intend on setting up the scene in the inspector. I figured I'd offer both methods in the answer and let Yharooer choose the one that fit. I do like the drag and drop approach myself.
@sdgd sorry mate, I'm not entirely sure what you are trying to say there.
Thanks Ellander, but when you say "If the script is/isn't a game manager and attached to an object," what exactly do you mean by game manager?
--Never $$anonymous$$d - I got it to work using the first solution. Thanks Heaps!
Answer by robertbu · Feb 23, 2013 at 09:45 AM
'Prefab' is something specific in Unity. It gets used on this list to mean "anything we are making copies of," but in reality there is a specific process to make a prefab. A prefab does not live in the hierarchy, so your problem of the spawn object being destroyed will not happen with a true prefab. Here is a link that walks through a couple of prefab creation scenarios:
http://docs.unity3d.com/Documentation/Manual/InstantiatingPrefabs.html
Thanks for your answer, - I've taken a brief look over the Rocket one, but with the rocket
variable, in the inspector, what would you attach that to?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to spawn a 'boss' after all enemies defeated and then kill that 'boss'? 1 Answer
copy enemy prefabs 1 Answer
Can't remove instantiated prefab 0 Answers