• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Moving grabbed object OnTriggerStay2D 0 Answers

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

NullReferenceException when trying to access parent 0 Answers

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges