how to increment a int when GameObject destroyed C#

Hello friends,
i have looked all over the forums and answers and i can’t find the answer to my question! So, here it is :
i need to increment a Integer when an object is destroyed. i have a perlin noise function to make my blocks and i want it so that when i destroy one i get more blocks in my “inventory”. i do not have a Gui i simply want to increment a public int. i am doing this in C#. i have made it so when you click on a block it destroys that block and makes one beneath it. as to not kill my computer. like in Minecraft.
so in short i need to increment an Int when one of my blocks is destroyed.
Thanks!

Just add the increment before where ever you destroy the block:

`
if block clicked
{
myInt += 1;
Destroy(block);
}`

It should always increment when object is destroyed.

Hey there. You could create a new script and add it to a persistent gameobject with the int variable and a method for incrementing to be called just before you destroy the object.

For example :
:

 using UnityEngine;
 using System.Collections;
 
 public class YourClass : MonoBehaviour {
     public int destroyed = 0;
     
     public void Destroyedcount {
         destroyed++;

     }
 }

Then call the method in the script you use to destroy the gameobject.

Hope that helps :slight_smile:

There are many ways you can do that here is one
If you have a script on the object you want to destroy you can use void OnDestroy (){ myInt++ }