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
1
Question by gRntaus · Jan 31, 2013 at 10:58 PM · gameobjectprefab

Attaching different prefabs via code?

I'm not sure if you can do this but I'm hoping it's possible. I'll use the current example although this isn't exactly what I am doing in game. Basically depending on a certain event (it might be a mouse click or if player hits a certain score or whatever) I want to be able to instantiate different Prefabs into my scene. Normally if I am working with once scene I would do something along the lines of the following:

 public gameObject enemy;
 
 
 ....... later in code
 
 
 private gameObject clone = Instantiate Object off enemy prefab;

I'd then drag the prefab onto the public gameobject in the scene view and things would work fine and I could then interact with "clone" from there on out without dealing with the prefab directly.

However I've run into an issue where I want to spawn a different enemy based on the situation. Lets say I have the following Prefabs in my Heirachy.

Enemy, Enemy_Special, Enemy_SuperSpecial

I'm making them up but go with me. How can I do something like the following?:

 //This can be set to null initially that's fine
 private gameObject enemy;
 
 public gameObject getEnemy() {
 
 //If certain condition is met pick the appropriate enemytype from the prefabs in my heirachy.
 
 enemy = Whatever GameObject the scripts says is necessary
 
 return enemy;
 
 }

I realise I could add a whole bunch of public gameObjects and add the specific enemies to their respective slots in the inspector but it's probably going to be a little messy that way and I can't guarantee how many objects I'll end up having it could be 3 or it could be 20. Surely I can add a Prefab via code. If so can someone explain in simple terms how I've looked around for ages but just don't understand how to do it.

Comment
Add comment
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

2 Replies

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

Answer by aldonaletto · Jan 31, 2013 at 11:28 PM

An alternative to set fields in the Inspector is to load the prefabs with Resources.LoadAll. I've not tested this, but suppose that you should place the enemies in a subfolder of Assets/Resources - say, Assets/Resources/Enemies - then get all of them in an Object array at Start.
Another possibility: save the enemies in Resources and simply load the enemy you want with Resources.Load - for instance:

    string enemyType;
    if (condition1){
      enemyType = "Ogre";
    } 
    else 
    if (condition2){
      enemyType = "Dragon";
    }
    else 
    if (condition3){
      enemyType = "Skeleton";
    }
    GameObject clone = Instantiate(Resources.Load(enemyType));


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 gRntaus · Jan 31, 2013 at 11:50 PM 0
Share

I've upvoted this as I think it will be the most useful but I'll wait until I get home to test that it works, I think this will be the most elegant but I'll work on a similar foundation and post back if I've had to make any changes.

avatar image
0

Answer by Duxten · Jan 31, 2013 at 11:29 PM

You can declare an array of Prefabs and instantiate it

var Enemies : GameObject[]; // Array of different Enemies that are used.

var obj : GameObject = Enemies[1]; <--- put inside the [] the number of the enemy you want

Instantiate(obj, pos.position, pos.rotation);

just an example you could make your own changes

Comment
Add comment · 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

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

Prints correct on console but not updating on Prefab/Gameobject. 2 Answers

Is an object created when dragging a prefab to a GameObject script field 1 Answer

How to create and save a gameobject to a prefab with a script 3 Answers

Calling script on prefab using array of prefabs. 1 Answer

Switch the prefab of a game object from a script 2 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