Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by davidflynn2 · May 21, 2013 at 05:30 PM · c#parent

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image clunk47 · May 21, 2013 at 05:42 PM 3
Share
 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.

avatar image davidflynn2 clunk47 · May 21, 2013 at 05:56 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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
 
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image clunk47 · May 21, 2013 at 05:55 PM 1
Share

To UNPARENT, set parent as NULL.

this.transform.parent = null;

avatar image davidflynn2 · May 21, 2013 at 05:57 PM 0
Share

Ya I thought of that right after I posted that lol.

avatar image davidflynn2 · May 21, 2013 at 05:57 PM 0
Share

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.

avatar image clunk47 · May 21, 2013 at 05:57 PM 0
Share

Thumbs Up Bro :)

avatar image ExTheSea · May 21, 2013 at 06:00 PM 0
Share

:D Exchanging thumbs up's

avatar image
3

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
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image penta_d · Sep 23, 2015 at 09:05 AM 0
Share

this saved me... thanks :)

avatar image YoungDeveloper penta_d · Sep 23, 2015 at 09:08 AM 0
Share

You can also use GO1.Setparent(GO2);

avatar image
0

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!");
         } 
     
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Can someone help me understand this code. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges