I have a gui button set up and what I am wanting to do is when it is pressed spawn and object and parent the third person controller to that object. I have never done this and am not sure how to start.
If you mean “parenting two gameobjects” you have to do something like this:
GameObject GOone;
GameObject GOtwo;
void ...(){
GOone.parent = GOtwo; //GOone is now the child of GOtwo
}
but if you mean: “attach a script/Component to a Gameobject” you have to do something like this:
GameObject GO;
void ...(){
GO.AddComponent(MouseLook); //Adds the MouseLook-Script to the Gameobject
}
Had same issue, this is what worked for me:
Transform GO1 = GameObject.find("GameObject1").transform;
Transform GO2 = GameObject.find("GameObject2").transform;
void... () {
GO1.parent = GO2; //GO1 now child of GO2
}
Had some issue with the above code so this is what worked for me. Hope this helps.
public class Cube : MonoBehaviour {
GameObject platform;
GameObject player;
// Use this for initialization
void Start () {
platform= GameObject.Find("platform");
player = GameObject.Find("FPSController");
}
void OnTriggerEnter(Collider other){
player.transform.parent = platform.transform;
Debug.Log("Parented!");
}
void OnTriggerExit(Collider other){
player.transform.parent = null;
Debug.Log("Unparented!");
}
}