Hello,
I just recently started Unity and am now starting to script with Javascript. I currently have this code so far that I found on here:
function OnTriggerEnter(collisionInfo : Collider){
Destroy (gameObject);
}
This destroys the object but I want to be able to show it in GUI Text on the top left. Does anyone know how to score the amount of objects you destroy/take?
you need an object somewhere that stores a variable (in this case an int would likely do). then just before your call to destroy() call a function on this object to increase or decrease the score appropriately. For example, place this script on an empty game object in your scene.
public ScoreKeeper : MonoBehaviour
{
public static int Score;
void Awake()
{
Score = 0;
}
void OnGUI()
{
GUI.Label(new Rect(0, 0, 100, 25), "Score: " + Score);
}
}
Then since score is a static variable, you can just change it like this.
ScoreKeeper.Score++;