- Home /
Question by
eibonscroll · May 26, 2011 at 04:08 AM ·
runtimeattach
Attach Contoller At runtime
I want to have a choice of what GameObject I attach a Controller to. Any suggestions on how this would be done.
Searching the scripting referance for Attach, AddComponent, Parent, didn't seem to turn up a function I could use to Dynamically add a Component at runtime
Mouse pick is the second issue, to get the object to attach to. But, that I think is more straight forward.
Thanks
Comment
Best Answer
Answer by eibonscroll · May 26, 2011 at 05:07 AM
Thanks, yes I found it a bit later. This did it.
if(!isAttached) { Collider[] coliders = Physics.OverlapSphere(transform.position, 5);
foreach(Collider c in coliders)
{
//print(c.ToString());
if( c.gameObject.tag.ToString() == "Attachable"){
c.gameObject.AddComponent("MyController");
isAttached = true;
}
}
}
Your answer