- Home /
How do I destroy an object after giving the player a point?
Hi, so I'm making a game, and I want to make these collectables that give points and then disappear. I tried making a script like this, and it disappears, but doesn't give the point. Could someone please help me?
public class Points : MonoBehaviour {
public TMP_Text MyText;
private int score;
// Use this for initialization
void Start()
{
MyText.text = "";
}
// Update is called once per frame
void Update()
{
MyText.text = "" + score;
}
void OnTriggerEnter(Collider other)
{
score = score + 1;
Destroy(gameObject);
}
}
Answer by justinenayat · Oct 09, 2021 at 07:15 PM
How is your score declared in the game? It seems like you have this script on the object that gets destroyed after the player collides with it. You've declared score as a private variable within I'd assume several different 'collectibles' objects. Instead, declare the score as a variable on the player or a game manager object. Then on the collision have the script on the player or game manager add +1 to the score.
Answer by WeirdWeirdo56 · Oct 10, 2021 at 09:49 AM
Thanks! There were many more mistakes along the way, but I finally got it to work