Hi, all,
I am looking for a script that will instantiate a dead replacement object as a child of a parent.
Any help out there? Am I close?
deadReplacementPosition = hit.transform.position;
deadReplacementRotation = hit.transform.rotation;
var parentTransform : Transform = hit.transform.parent.gameObject.transform;
Destroy( hit.collider.gameObject );
// Play a dying audio clip
if (dieSound) {
AudioSource.PlayClipAtPoint(dieSound, transform.position);
}
// Replace ourselves with a deadbody
if (deadReplacement) {
var dead : GameObject = Instantiate(deadReplacement, deadReplacementPosition, deadReplacementRotation);
dead.transform.parent = parentTransform;
}
Can you tell us what kind of behavior you’re seeing with this script? It might help guide people’s guidance.
I noticed a couple things. I think var parentTransform : Transform = hit.transform.parent.gameObject.transform; is redundant. Transform.parent will give you a Transform; it’s not necessary to go back out to get the Transform of the GameObject of the Transform you’re looking for. You might be doing something similar with “hit.” What kind of object is “hit?”
If you’re not seeing the replacement object, it may be because this script is already attached to the parent object (where the collider might be for a complex avatar), rather than some child component and you’re therefore destroying the very thing you’re trying to attach your replacement to, but it’s hard to tell from the information here.
Thanks, Burnumd,
This is the code that I’m using with the above- as you can see it is placed at the end. I am using this script on a child object.
What I’m trying to do is simply replace one child object with another via this script.
Any other suggestions?
Thanks for your time!
Greg
var deadReplacement : Rigidbody;
var deadReplacementPosition : Vector3;
var deadReplacementRotation : Quaternion;
var dieSound : AudioClip;
var explosion : Transform;
var damagesound : AudioClip;
var initialDelay = 0.0;
var fadeSpeed = 1.0;
var explosionRadius = 5.0;
var explosionPower = 0.0;
var explosionDamage = 25.0;
var explosionTime = 1.0;
var myTransform : Transform;
var hit : RaycastHit;
function Start () {
var explosionPosition = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
for (var hit in colliders) {
if (!hit)
continue;
Debug.Log( " hit.collider.tag = " + hit.collider.tag);
if (hit.rigidbody hit.collider.tag == ("Respawn") ) {
Debug.Log(" respawn object hit");
var closestPoint = hit.ClosestPointOnBounds(explosionPosition);
var distance = Vector3.Distance(closestPoint, explosionPosition);
var hitPoints = 1.0;
hitPoints *= explosionDamage;
hit.rigidbody.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
break;
}
}
}
renderer.material.color.a = 0.0;
yield new WaitForSeconds(initialDelay);
while (renderer.material.color.a < 1.0) {
color = renderer.material.color;
color.a = 1.0;
renderer.material.color = Color.Lerp(renderer.material.color, color, fadeSpeed * Time.deltaTime);
yield;
}
deadReplacementPosition = hit.transform.position;
deadReplacementRotation = hit.transform.rotation;
var parentTransform : Transform = hit.transform.parent.gameObject.transform;
Destroy( hit.collider.gameObject );
// Play a dying audio clip
if (dieSound) {
AudioSource.PlayClipAtPoint(dieSound, transform.position);
}
// Replace ourselves with a deadbody
if (deadReplacement) {
var dead : GameObject = Instantiate(deadReplacement, deadReplacementPosition, deadReplacementRotation);
dead.transform.parent = parentTransform;
}
First,
can be simplified to the much easier to read
if (hit hit.rigidbody hit.collider.CompareTag("Respawn")) {
Debug.Log( " hit.collider.tag = " + hit.collider.tag);
//...
I have a few more questions:
Can you tell me exactly what is happening?
What parts of your code are being executed?
One thing that jumps out at me, is that bit at the end inside a function in your actual script? Is it intended to be inside Start()?
Does this script get attached to the thing being exploded via AddComponent() or is it attached to the explosion prefab?
Hi, Burnumd, thank you so much for your time and thought into my questions. I thought it might be easier to show you, (and others) rather than try to feebly explain without an example.
If you can import the attached Unity package into a project you will see a simple 2D scene. There are four green dots within a sun object. When pressed, each green dot subtracts hitpoints from the sun and is replaced by a gold dot. Upon death, the sun disappears.
What I want to do is call another dead replacement (Dot replacement 2) three seconds after the gold dot instantiates, effectively destroying the gold dot and replacing it with the object called: Dot replacement 2.
To recap the order of events: Press green dot, dot destroys self then fades up gold, 3 second pause then gold dot replaced with another green dot (Dot replacement 2). - it is this last step that I cannot do. I can’t seem to instantiate the second replacement green dot and make it a child of the sun object.
Sorry if this is convoluted, but it’s the only way I can think to explain my problem at the moment.
Thanks for your help!
Greg
183688–6515–$exercise_1_788.unitypackage (485 KB)