Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by samrules14 · May 18, 2017 at 12:36 AM · gameobjectparentchildclassgeneration

Creating a child gameObject from a parent script while running (aka not using eg. start() or Selection)

Hello... First question:

I'd like to have a parent "TreeCreator" class declare, initialize, and spawn a new child gameObject, "Heart" with the component heart.cs (I saw the addComponent thing so that shouldn't be a problem).

Here's what I have so far (from another example):

 [MenuItem("GameObject/Create Empty Child #&c")]
     // Use Shift+Alt+c
     // For initialization
     static void createEmptyChild () {
         GameObject heart = new GameObject("Heart");
         Instantiate (heart,     
             Vector3(Transform.position.x, Transform.position.y, Transform.position.z),
             Quaternion.identity);
         heart.transform.parent = tree.activeTransform.parent;
         heart.transform.Translate(Selection.activeTransform.position);
     }

I get an error for tree.activeTransform.parent //UnityEngine.GameObject doesn't contain this I understand from another tutorial that if I did "Selection.activeTransform.parent" it would work but I don't want to have to select each object that I want to create a child in because I will be generating thousands of these "TreeGeneration" gameObjects all with their own "Heart" class.

Any help or push in the right direction is greatly appreciated.

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 samrules14 · May 14, 2017 at 06:23 PM 0
Share

Oh also I did declare the tree already... Here it is:

 GameObject tree = this.gameObject; //This is an error because of "this"
avatar image samrules14 · May 14, 2017 at 08:28 PM 0
Share

Figured it out: $$anonymous$$ods please still verify question because there isn't a clearer example for parent script to child creation then this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 public class TreeCreator : $$anonymous$$onoBehaviour {
 
     void Start(){
         createHeart ();
     }
         
     void createHeart () {
         GameObject heart = new GameObject ("Heart");
         heart.transform.parent = this.transform;
         heart.transform.position = this.transform.position;
         if (this.transform.parent != null) {
             Destroy (heart);
             Destroy (this);
             Debug.Log ("Deleted tree and heart due to incorrect referencing.");
         } else {
             Debug.Log ("Tree -> Heart Created", this);
         }
     }
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Yuzuke · May 18, 2017 at 01:13 AM

heart.transform.SetParent( tree.transform );

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

121 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can not access GetComponentInParent ( ). usegravity 1 Answer

NullReferenceException when trying to access parent 0 Answers

How to set GameObject created by MenuItem as a child of Canvas 0 Answers

Moving grabbed object OnTriggerStay2D 0 Answers

How to add a texture to all the child game objects under a parent ? 0 Answers


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