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 Mincemeat · May 02, 2014 at 10:55 AM · instantiatecomponents

Adding an unkown Component type to an Instantiated GameObject

First Time using this, so I'm sorry if the question is too vague or formatted incorrectly.

Question: I need to Add a component to a Game Object that I will instantiate. This Component's name AND type are unknown at run time, I therefore have to find a way to Find the Type of the component on the original Game Object and then add that Component to the newly Instantiated Game Object.

I'm very sorry if that was confusing I sometimes have a hard time explaining things.

Comment
Add comment · Show 1
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 Owen-Reynolds · May 02, 2014 at 01:49 PM 0
Share

Seems like this isn't needed -- Instantiate copies all components anyway. Even if you have some gameObject "Phil," add an arbitrary component at runtime, and then Instantiate from Phil, that new component will be copied.

But In general, "unknown type" situations usually means you're thinking about the problem in an awkward manner. At worst they can be rewritten into inheritance with $$anonymous$$ subclasses.

3 Replies

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

Answer by jeebxoxo · May 02, 2014 at 02:22 PM

Hard to say because you're right this question is a little vague. There has to be a piece of code that handles this somewhere and I'm not sure where you want that. I'll give it a shot though.

So it seems like you have GameObject One that could have a variety of components on it. Can you have a handler GameObject that finds this and creates the new one or does the 'One' GameObject create the new one?

Anyway, I'm guessing something is happening in your game that interacts with the 'One' GameObject so getting a reference to it won't be a problem. If the script handling this knows nothing you're going to need to set up something to iterate through all the possible options to find which component it has. If you had an array of all the possible component names in strings something like this would work:

your array[] could be called whatever you want, i'll call mine 'comps' for now. The initial GameObject I'm calling 'One'. What would be nice is that if this list expanded, you could set up a script that you type in all of them and then it creates the array for you when you start up your game.

 foreach(string c in comps) {
 
 if (One.GetComponent<c> != null) {
 // setup reference to found component -- this is definitely psuedo code and should be tweaked to properly check for the component**
 }
 
 }
 
 //Instantiate the GameObject you want with the found reference

I've never done dynamic component lookup with strings myself, so I'm not sure if that code would work properly--it's just off the top of my head. You may need to do some casting in order to set up the references properly.

  • Check this link out about casting, it should help you: http://forum.unity3d.com/threads/193577-c-GetComponent-string

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

Answer by theredace · May 02, 2014 at 02:44 PM

Would it be easier to just instantiate a prefab with all possible components (or one of several possible prefabs) and then just disable the components you don't need after instantiation?

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

Answer by Mincemeat · May 03, 2014 at 12:23 AM

Alright Was hoping that there was an actual function built in to deal with this kind of thing, or at least semi deal with it. But I guess not. Thanks Jeeb for trying to help I'll mark your answer as correct, as I ended up doing a string based solution.

             public Transform[] PopulateSpellArray(Transform[] ChildArray)
             {
                 ChildArray = new Transform[transform.childCount];
                 for (int i = 0; i < transform.childCount; i++)
                 {
                     ChildArray[i] = transform.GetChild(i);
                 }
                 return ChildArray;
             }
     
             public void AssignScriptToProjectile(GameObject Projectile, Transform[] SpellsArray)
             {
                 //Need to find a way to add the Components on the child to the Projectile, if that doesnt work Child the CHildren to the projectile
                 Projectile = Missile;
     
                 string[] ComponentsArray = new string[gameObject.transform.childCount];
                 for (int i = 0; i < gameObject.transform.childCount; i++)
                 {
                     ComponentsArray[i] = gameObject.transform.GetChild(i).gameObject.name;
                     Debug.Log(ComponentsArray[i]);
                 }
     
                     foreach (string n in ComponentsArray)
                     {
                         Projectile.AddComponent(n);
                     }
 
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

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

Related Questions

How to update a script to a network script 1 Answer

Retrieving Script component from gameobject. 0 Answers

Will Instantiating prefab with many components slow my game? 1 Answer

instantiating multiple objects and assigning them individual values 2 Answers

Clickable GameObjects within one another... 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