var speed = 50.0f;
var score = 0;
function Update(){
transform.position.x += speed * Time.deltaTime;
GameObject.Find("Score").guiText.text = "Your score: " +score;
}
function OnCollisionEnter(col : Collision){
if(col.gameObject.name == "EnemyRight"){
score += 5;
Destroy(col.gameObject);
Destroy(gameObject);
}
else if(col.gameObject.name == "EnemyLeft"){
score += 5;
Destroy(col.gameObject);
Destroy(gameObject);
}
else if(col.gameObject.name == "EnemyRight(Clone)"){
score += 5;
Destroy(col.gameObject);
Destroy(gameObject);
}
else if(col.gameObject.name == "EnemyLeft(Clone)"){
score += 5;
Destroy(col.gameObject);
Destroy(gameObject);
}
}
What’s wrong with my code, it collide but score isn’t increasing
I would try using a timer before using the Destroy, as it may not be able to increase the score before the destroy is executed.
try adding this to the code:
var DestroyTimer : float = 0.01;
var DestroyTimerHolder : float = 0.01;
funciton OnCollisionEnter(col : Collision)
{
if(col.gameObject.name == "EnemyRight")
{
score += 5;
DestroyTimer -= Time.deltaTime;
if(DestroyTimer == 0.00 || DestroyTimer < 0.00)
{
DestroyTimer = DestroyTimerHolder;
Destroy(col.gameObject);
Destroy(gameObject);
}
}
}
Just add that code over there and apply it to every collision (enemy left, enemy right, enemy right clone, enemy left clone)
Basically what this does is that whenever you collide with those objects, the timer will go off and when it hits 0 or goes smaller than 0 it will destroy and then set the timer back to how it was. if you modify the number of the main timer make sure to modify the number also on the timer holder. I have not tried this script so i do not know if this will work.