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 Chris50o · Mar 01, 2017 at 07:23 AM · 2d game

How can I Instantiate a parent object in a child object?

I am creating a code that makes an enemy drop items, but the problem is that I dont want to instantiate the item as a child of the enemy

For example:

Enemy1 <-- Parent (Then, when its killed generates a child object, which is the item)

Item <-- Child of enemy1

And since the enemy1 was killed, it will be destroyed, but the problem is that it also destroys the Item that it drops, since its a child from enemy.

The code I am using for this is:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EnemyControl : MonoBehaviour , ILivingThing{
 
     public Enemy thisEnemy;
     
     void Update () {
         //Some artificial intelligence here...
         Damage(1);
     }
 
     public void Damage(float damageAmt){  //Implemented from ILivingThing
         thisEnemy.Damage (damageAmt);
         if (thisEnemy.Health <= 0) {
             dropItem ();
             Kill ();
         }
     }
 
     public void Heal(float healAmt){    //Implemented from ILivingThing
         thisEnemy.Heal (healAmt);
     }
 
     public void Kill(){    //Implemented from ILivingThing
         thisEnemy.Kill ();
         //Destroy (gameObject);
     }
 
     void dropItem(){
         GameObject Item = Instantiate (Resources.Load ("Prefabs/Items/Item1"), gameObject.transform) as GameObject;
         Item.GetComponent<ItemControl> ().thisItem = new Item1();
     }
 }

How can I Instantiate a GameObject on "top of hierarchy" to get rid of this problem?

Comment
Add comment
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

1 Reply

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

Answer by Cycy · Mar 01, 2017 at 01:45 PM

Hi, here the doc https://docs.unity3d.com/ScriptReference/Object.Instantiate.html.

It says :

 public static Object Instantiate(Object original, Transform parent);

So if you want to instantiate your GameObjet on top of hierarchy (ie : a GameObject with no Parent), your dropItem() could be :

 GameObject Item = Instantiate (Resources.Load ("Prefabs/Items/Item1"), null) as GameObject;

In a general way :

 transform.parent = null; // will move the transform to the root of your scene.

Hope it will help you,

bye :)

Comment
Add comment · Show 1 · 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 Chris50o · Mar 02, 2017 at 06:33 AM 0
Share

Thank you very @Cycy !

I've checked the unity's documentation, but I didn't know how to implement this: "By default the parent of the new object will be null, so it will not be a "sibling" of the original. However, you can still set the parent using the overloaded methods."

Now, I understood it, its just about setting null the transform.

Thanks!!

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

98 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

Related Questions

Cannot get my double jump to work (2D) , 0 Answers

After upgrading from 2019.1.7 to 2019.4.8, some graphics on iOS are not displaying correctly 0 Answers

2D box collider always detecting player,2D Box colliders detecting player when he is not there 1 Answer

Unity Linerenderer jaggy lines 0 Answers

How to get collision on a Collider with "is Trigger" on? 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