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;
}
}