- Home /
Parent Object Via Code C#
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.
using UnityEngine;
using System.Collections;
public class EXA$$anonymous$$PLE : $$anonymous$$onoBehaviour
{
public GameObject controller;
public GameObject prefabToSpawn;
void OnGUI()
{
if(GUIButton(new Rect(0, 0, 64, 64), "Spawn")
{
GameObject clone = Instantiate(prefabToSpawn, Vector3.zero, Quaternion.identity);
controller.transform.parent = clone.transform;
}
}
}
This is a simple EXA$$anonymous$$PLE on how to do something like this. This is not a full script, you need to change things to your liking. What I did here, is create 2 public game objects which will appear in the inspector for you to drag and drop an object into. Drag your 3rd person controller into the controller slot, and the prefab you want to instantiate into the prefabToSpawn slot. Be sure to CLONE the prefab, do not instantiate the prefab itself. This is where GameObject clone = Instantiate(prefabToSpawn...) comes in. Then to parent your controller to this CLONE, controller.transform.parent = clone.transform; Hope this helps.
I would like to give you both the mark of correct answer because you both had a part of the code I needed. So I gave you both the thumbs up. Thanks For your help.
Answer by ExTheSea · May 21, 2013 at 05:44 PM
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
}
To UNPARENT, set parent as NULL.
this.transform.parent = null;
I would like to give you both the mark of correct answer because you both had a part of the code I needed. So I gave you both the thumbs up. Thanks For your help.
Answer by xero19 · Oct 23, 2013 at 08:30 PM
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
}
Answer by egwillfriedel · Jun 10, 2016 at 12:09 PM
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!");
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Die Function Help With C# 1 Answer
touch to instantiate and move object ? 1 Answer
Distribute terrain in zones 3 Answers