Add a tag to the collectible object, e.g. “food”.
Add a trigger collider to the collectible object.
If you want to use PlayerPrefs to keep your score, attach this script to the player:
void OnTriggerEnter (collider c) {
if (c.transform.tag == "food") {
Destroy(c.gameObject);
addScore(your_score, 10); // add your own score
}
}
void addScore(string variable_name, int points_to_add) {
PlayerPrefs.SetInt(variable_name, PlayerPrefs.GetInt(variable_name) + points_to_add);
}
If you’re fine with a normal variable, attach this script:
int your_score = 0;
void OnTriggerEnter (collider c) {
if (c.transform.tag == "food") {
Destroy(c.gameObject);
your_score += 10; // add your own score
}
}
Make sure that your player also has a trigger collider, and that at least one of the two (player or collectible) has a rigidbody attached.