- Home /
Other
Adding score on collision not working
So I have a script that detects collision with a cube that script is
#pragma strict
var score : int = 1;
var amount : int = 1;
var Collide : boolean;
var currentScore : UnityEngine.UI.Text;
function Start () {
PlayerPrefs.GetInt("Score");
}
function OnCollisionEnter(collision : Collision){
Debug.Log( "Collision! collision.gameObject.name = " + collision.gameObject.name);
if(collision.gameObject.name == "Cube"){;
Destroy(gameObject,0);
score += 1;
}
if(collision.gameObject.name == "Platform"){;
Debug.Log("Score: " + score);
Time.timeScale = 0;
}
}
function Update () {
PlayerPrefs.SetInt("Score", score);
currentScore.text = "" + score;
}
I want to be able to increase the score by 1 on collision but when it collides the only thing that happens is that it destroys the gameObject and doesn't add any score.
Answer by DoTA_KAMIKADzE · Apr 19, 2015 at 10:34 PM
I can't guess what you want to do, but probably one of 2 things:
1) Destroy the cube instead of itself. Then:
if(collision.gameObject.name == "Cube")
{
Destroy(collision.gameObject);
score += 1;
}
2) You want to destroy the holder of the script. Then:
if(collision.gameObject.name == "Cube")
{
score += 1;
Destroy(gameObject);
}
Also make your score variable static. Note that your code will no longer continue to run after its holder will be removed after the end of all that processing, so you will need to save and set new value to text from some other running script.
Follow this Question
Related Questions
Whats wrong with my score script 1 Answer
Most efficient way to get scores 1 Answer
Question about Point adding / Destroying after collision. 0 Answers
Count scripting problem 1 Answer
Score count increase on hit 2 Answers