Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Yharooer · Feb 23, 2013 at 09:26 AM · instantiateprefabdestroyenemy

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.)

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Yharooer · Feb 23, 2013 at 09:20 AM 0
Share

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?

avatar image Ellandar · Feb 23, 2013 at 09:45 AM 0
Share

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?

avatar image Yharooer · Feb 23, 2013 at 10:54 AM 0
Share

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.

avatar image Yharooer · Feb 23, 2013 at 11:09 AM 0
Share

(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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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. alt text


prefabs.jpg (178.9 kB)
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Feb 23, 2013 at 04:05 PM 0
Share

@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.

avatar image sdgd · Feb 23, 2013 at 04:13 PM 0
Share

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

avatar image Ellandar · Feb 23, 2013 at 04:48 PM 0
Share

@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.

avatar image Yharooer · Feb 23, 2013 at 09:01 PM 0
Share

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!

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Yharooer · Feb 23, 2013 at 11:01 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

how to destroy camera instatiated from prefab? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges