A HUGE technical problem with making AddComponent(string) obsolete.

Hello,
I am writing because I really liked how I USED TO be able to use AddComponent(String) and GetComponent(String) in my game development, and the reason is because of systems like UMA (a random character and avatar creator). Every time UMA creates a 3D character, its customized skeleton does not get added (as a child) until seconds later, and I believe that each skeleton is based on a prefab or something I haven’t taken the time to explore because UMA’s system is so complex.

I wrote a script called, “attachScr” which has a variable for WHEN the skeleton gets loaded, what is the limb name to add a script to, and what is the name of the script to add WHEN that limb is available (when the skeleton loads). For example, I would have a script called “MouseMove” for the “head” of each skeleton that gets created by UMA, but without the ability to use AddComponent(String) and GetComponent(String), I would have to manually attach the “MouseMove” script myself to every “Head” of every skeleton that gets created. Is there a Unity add on to UN-obsolete it? Here is what I had so far, and my game worked fine until I downloaded the latest version of Unity.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class attachArgs {
    public GameObject playerObj;
    public GameObject functionsObj;
    public GameObject target;
    public string childName;
    public GameObject settingsObj;
    public List<modCharacterInfo> modCharacters;
    public float mouseSensitivity;
    public AudioClip soundClip;
}

public class attachScript : MonoBehaviour {

    public attachArgs args;

    public string attachScr;
    public string name;

    private bool attached = false;
    private GameObject limb;
   
   
    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {
        if (!attached) {
            limb = findObject(name, gameObject);
            if (limb != null) {
                if (!limb.GetComponent(attachScr)) {
                    limb.AddComponent(attachScr);
                    /*
                    if (attachScr == "AIFollow") {
                        limb.GetComponent<CharacterController>().center = new Vector3(0,1,0);
                        limb.GetComponent<AIFollow>().speed = 1;
                        limb.GetComponent<AIFollow>().target = GameObject.Find ("Male_Unified").transform;
                    }*/
                    if (attachScr == "moveto") {
                        limb.GetComponent<moveto>().speed = 0.02f;
                        limb.GetComponent<moveto>().target = findObject(args.childName, args.target);
                        limb.GetComponent<moveto>().settingsObj = args.settingsObj;
                        limb.GetComponent<moveto>().doorSpace = 1;
                    }
                    if (attachScr == "posmod") {
                        limb.GetComponent<posmod>().settingsObj = args.settingsObj;
                        limb.GetComponent<posmod>().modCharacters.Clear();
                        for (int i = 0; i < args.modCharacters.Count; i++) {
                            modCharacterInfo m = new modCharacterInfo();
                            m.orig = args.modCharacters[i].orig;
                            m.find = args.modCharacters[i].find;
                            limb.GetComponent<posmod>().modCharacters.Add(m);   
                            limb.GetComponent<posmod>().isPlayer = true;
                            limb.GetComponent<posmod>().functionsObj = args.functionsObj;
                            limb.GetComponent<posmod>().playerObj = args.playerObj;
                        }
                    }
                    if (attachScr == "MouseLook2") {
                        limb.GetComponent<MouseLook2>().axes = MouseLook2.RotationAxes.MouseX;
                        limb.GetComponent<MouseLook2>().sensitivityX = args.mouseSensitivity;
                    }
                    if (attachScr == "stomp") {
                        limb.GetComponent<stomp>().sound = args.soundClip;
                    }
                    if (attachScr == "monster") {
                        limb.GetComponent<monster>().mainPlayerObj = args.playerObj;
                        limb.GetComponent<monster>().playerName = args.childName;
                        limb.GetComponent<monster>().roarSnd = args.soundClip;
                    }
                }
                attached = true;
            }
        }
    }
   
    public GameObject findObject(string name, GameObject obj) {
        GameObject robj = null;
        foreach (Transform child in obj.transform) {
            if (child.name == name) {
                robj = child.gameObject;
                return robj;
            } else {
                robj = findObject (name, child.gameObject);
                if (robj != null) {
                    return robj;
                }
            }
        }
        return robj;
    }

}

I think you posted in the wrong forum.

While I’m here though… you should be able to get a type from a string still using Type.GetType(string name, string assembly) if you really need to. In the end though, I don’t imagine this string to Type being all that fast, and probably deprecated so people don’t use it by mistake.

Say you have a MonoBehaviour script/subclass called SuperMuscles, here’s a way to add it to a gameObject in Unity 5:

string behaviourName = "SuperMuscles";
System.Type behaviourType = Type.GetType(behaviourName);
gameObject.AddComponent(behaviourType) as SuperMuscles; // as SuperMuscles is only there so you can do: 'var superMuscles = gameObject.AddComponent(behaviourType) as SuperMuscles'

(Note: Wrote this off the top of my head, so I haven’t tried it by I have used this solution myself.)

Thank you.