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 RaulG · Feb 24, 2015 at 01:46 AM · instantiateid

Spawn a prefab on another object based on ID?

How would I go about doing this? Are there any Tutorials or Information I can use?

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
0

Answer by maccabbe · Feb 24, 2015 at 02:08 AM

I think you are looking for switch/case or if/elseif/else statments. Not sure what you meant by spawn a prefab on another object so I assumed you wanted them to share position.

 public enum ID {
     Prefab1, Prefab2,
 }
 
 public class Script {
     public void SpawnComponent(ID id, GameObject gameObject){
         switch(id){
             case ID.Prefab1: {
                 GameObject newObj=Object.Instantiate(Resources.Load("Prefab1")) as GameObject;
                 newObj.transform.position=gameObject.transform.position;
                 break;
             }
             case ID.Prefab2: {
                 GameObject newObj=Object.Instantiate(Resources.Load("Prefab2")) as GameObject;
                 newObj.transform.position=gameObject.transform.position;
                 break;
             }
         }
     }
 }
 
Comment
Add comment · Show 3 · 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 RaulG · Feb 24, 2015 at 02:16 AM 0
Share

Im sorry but can you explain how that works? Or were you learned how to do it?

Thank you for the information!

avatar image maccabbe · Feb 24, 2015 at 02:34 AM 0
Share

You can learn what all this means by going though the unity tutorials at http://unity3d.com/learn/tutorials/modules/beginner/scripting. I would recommend going through all of them if you expect to be scripting.

The relevant parts in the tutorials are Beginner 27(enumeration), 2 (variables and functions), 28 (switch statements), 24(instantiate), and 13 (translate and rotate).

Afterwords learning what the tutorials offer you might still need to go through the documentation. For instance, I found how to edit the gameObject position directly at http://docs.unity3d.com/ScriptReference/Transform.htm.

avatar image RaulG · Feb 24, 2015 at 05:43 AM 0
Share

Okay.. The tutorials you gave me didn't come close to matching what you put..

What is Prefab1 and 2 suppose to be anyway? The object being spawned from the object, or the spawning object?

Why do you use Resource.Load and not just connect it through the inspector?

How does it know what to compare to?

avatar image
0

Answer by EggQuiz857 · Feb 24, 2015 at 02:35 AM

Just give the prefab the name of the id you want.

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 RaulG · Feb 24, 2015 at 02:40 AM 0
Share

And then? If I try to spawn in multiple objects, how would the instantiated object know which ID to go 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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding id variable # to instantiate-ed object 2 Answers

Getting instance ID from position 1 Answer

Assigning id to instantiated objects 1 Answer

Instantiate ID 2 Answers

Unique names for Instantiated prefabs? 1 Answer


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