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.

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

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?

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 *= 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 = gameObject.transform.GetChild(i).gameObject.name;
Debug.Log(ComponentsArray*);*
}

foreach (string n in ComponentsArray)
{
Projectile.AddComponent(n);
}