- Home /
Question by
exchangeSh9 · Nov 07, 2011 at 01:57 PM ·
gameobjectinstantiatevariablefunctionexplosion
Calling a variable
Hi this may sound like a very dumb question but I am trying to get the explosion to be made at the player location but only when the health reaches 0, here isi the script.
var explosion : GameObject;
var playerHealth = 100;
var damage = 10;
function OnCollisionEnter(collision : Collision)
{
var contact : ContactPoint = collision.contacts[0];
var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
var playerDead = Instantiate (explosion, contact.point, rotation);
if(collision.gameObject.tag=="objectA" || collision.gameObject.tag=="objectB" || collision.gameObject.tag=="objectC")
{
playerHealth -= damage;
}
}
function Update()
{
if(playerHealth <= 0)
{
//want to call the playerDead variable here if possible
Destroy(gameObject);
}
}
Thank you
Comment
Answer by syclamoth · Nov 07, 2011 at 02:03 PM
You just need to rearrange things a little bit.
In your OnCollision-
if(collision.gameObject.tag=="objectA" || collision.gameObject.tag=="objectB" || collision.gameObject.tag=="objectC")
{
playerHealth -= damage;
if(playerHealth <= 0)
{
// Instantiate the explosion, delete the object
}
}
Then just get rid of the thing in Update (since it is now unnecessary)
Dude thank you for the help, I made those adjustments and it is working now :) also cheers Ludiares for the help mate
Answer by Ludiares.du · Nov 07, 2011 at 02:03 PM
Try Instantiting the explosion in the if block, not inside the OnCollisionEnter function