- Home /
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
???
Could it be that the replacement part does instantiate, but is destroyed again when the current object's parent is destroyed?
@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)
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
???
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.
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).
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);
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.)