I am making pong game and when my ball hits the box it’s health will not go down. Then once health is lower than 1 it should die.
My health will not go down? The ball obj is called Ball and also is tagged as Ball. What’s the problem? Thanks The white box should die after two hits.
Webplayer: https://dl.dropbox.com/u/56545886/Pong/Pong.html
public var boxHealth : int = 50;
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject == "Ball")
{
//Destroy(collision.gameObject); //destroy it
boxHealth -= 25;
}
if(boxHealth < 1)
{
Destroy(collision.gameObject); //destroy it
}
}
Just a guess: boxHealth = boxHealth - 25
Do you check the boxHealth in update?
Do you mean this?
function Update()
{
public var boxHealth : int = 50;
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject == "Ball")
{
//Destroy(collision.gameObject); //destroy it
boxHealth -= 25;
}
if(boxHealth < 1)
{
Destroy(collision.gameObject); //destroy it
}
}
}
function Update()
{
public var boxHealth : int = 50;
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject == "Ball")
{
//Destroy(collision.gameObject); //destroy it
boxHealth = boxHealth - 25 // LIKE THIS
}
if(boxHealth < 1)
{
Destroy(collision.gameObject); //destroy it
}
}
}
Assets/PongScripts/BoxHealth.js(4,9): BCE0044: expecting }, found ‘public’. ERROR
Assets/PongScripts/BoxHealth.js(28,5): BCE0044: expecting EOF, found ‘}’. ERROR
It should be:
collision.gameObject.Name/*or gameObject.Tag*/ == "Ball"
Otherwise you are just asking if the object is equal to the string “Ball”