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
0
Question by Digital-Phantom · Apr 10, 2015 at 09:11 AM · instantiatepositionobjectdestroyparent

Object won't instantiate at parents position(solved)

I'm trying to destroy part of my enemy and instantiate a damaged version of that part in its place (kind of a ragdoll effect).

I can destroy the object fine but the object I want to instantiate doesn't appear when the original object is destroyed.

 using UnityEngine;
 using System.Collections;
 
 public class DestroyClaw : MonoBehaviour
 {
 
     public GameObject invaderExplosion;
     public GameObject brokenClaw;
     public int clawHealth = 3;
 
 
     void OnTriggerEnter (Collider other) 
     {    
         if (other.tag == "Shot")
         {
             Debug.Log ("Hit Registered");
             clawHealth -= 1;
             Destroy(other.gameObject);
         }
     }
 
 
     void Update()
     {
         if(clawHealth <= 0)
         {
             Instantiate(invaderExplosion, transform.position, transform.rotation);
 
             GameObject item = GameObject.Instantiate(brokenClaw, transform.position, transform.rotation)as GameObject;
             item.transform.parent = gameObject.transform;
 
             MiniBossHealth.clawsAttached -= 1;
             Destroy(transform.parent.gameObject);
         }
     }
 }
 
 

I'm getting the Debug logs, the clawHealth is counting down on each hit, even the explosion instantiates but the replacement part doesn't appear?

Any ideas as to why

???

Comment
Add comment · Show 8
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 kevinspawner · Apr 10, 2015 at 09:26 AM 0
Share

Is it parented to the main object?

avatar image AeonIxion · Apr 10, 2015 at 09:28 AM 1
Share

Could it be that the replacement part does instantiate, but is destroyed again when the current object's parent is destroyed?

avatar image Digital-Phantom · Apr 10, 2015 at 09:37 AM 0
Share

@keveinspawner yes it does make itself a child to the object, but not the main parent.

@Aeonlxion I commented out the Destroy object line to see what would happen. The object did instantiate so yes, I'm guessing as you suggested it also gets destroyed with the object.

Also noticed another thing I hadn't planned on and that's without the destroy I get hundreds of them instantiating. Obviously need to move the instantiates out of my update and into a couroutine. (bit of a noob error there...oops)

avatar image Digital-Phantom · Apr 10, 2015 at 10:00 AM 0
Share

O$$anonymous$$ some of the issues resolved. Now only instantiating one instance of the prefab by moving things to a coroutine.

However I'm still destroying the instantiated object when I destroy the old object?

 using UnityEngine;
 using System.Collections;
 
 public class DestroyClaw : $$anonymous$$onoBehaviour
 {
 
     public GameObject invaderExplosion;
     public GameObject brokenClaw;
     public int clawHealth = 3;
 
     public bool clawGood = true;
 
 
     void OnTriggerEnter (Collider other) 
     {    
         if (other.tag == "Shot")
         {
             Debug.Log ("Hit Registered");
             clawHealth -= 1;
             Destroy(other.gameObject);
         }
     }
 
 
     void Update()
     {
         if(clawHealth <= 0  && clawGood == true)
         {
             ReplaceClaw();
             clawGood = false;
         }
     }
 
     void ReplaceClaw()
     {
         Instantiate(invaderExplosion, transform.position, transform.rotation);
 
         GameObject item = GameObject.Instantiate(brokenClaw, transform.position, transform.rotation)as GameObject;
         item.transform.parent = gameObject.transform;
 
         $$anonymous$$iniBossHealth.clawsAttached -= 1;
         Destroy(gameObject);
     }
 }
 
 

So how do I instantiate the object at the parents position but without it being a child of the parent

???

avatar image Blackup · Apr 10, 2015 at 10:37 AM 1
Share

Let me summarize. You have a claw object. - It gets hit by a "shot" and the shot is destroyed. - Then you wish to replace the claw with a different broken claw... - And this broken claw should remain attached to the body as the healthy claw was before?

your sequence of events should be like this. Instantiate explosion like you do above. Instantiate brokenClaw like you do above. Set the parent to gameObject.transform.parent then Destroy (gameObject)

Confirm for me if that is what you're looking for and I'll post it as a separate answer.

Show more comments

2 Replies

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

Answer by AeonIxion · Apr 10, 2015 at 10:48 AM

change

item.transform.parent = gameobject.transform

to

item.transform.parent = gameobject.transform.parent

the way it is now, you're making the new claw a child of the current object and then you destroy the current object (so the new claw is destroyed too).

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

Answer by Blackup · Apr 10, 2015 at 12:31 PM

This should do the trick.

 Instantiate(invaderExplosion, transform.position, transform.rotation);
 GameObject item = GameObject.Instantiate(brokenClaw, transform.position, transform.rotation)as GameObject;
 item.transform.parent = gameObject.transform.parent;
 MiniBossHealth.clawsAttached -= 1;
 Destroy(gameObject);
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 Blackup · Apr 10, 2015 at 10:56 AM 3
Share

Remember that if you destroy a parent object, you destroy all of the children (and grandchildren) too. they don't just become orphans... they die with the parent... (so on a separate note, if you want to make a child an orphan, you should set its parent value to null before destroying the parent. In other words, disconnect them. That way they don't die WITH the parent.)

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

21 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

Related Questions

Destroy and instantiate far objects 2 Answers

Keep instantiated GameObject's position while parenting it to another GameObject 3 Answers

how to raycast instantiated game characters 1 Answer

How to destroy instantiated objects. 1 Answer

Destroying object using his name and raycast 2 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