Spawning different items from different prefabs

Tipical bush of some Zelda with tipical reward for destroy it then i have this code to manage it:

private void OnTriggerEnter2D(Collider2D collision)
    {
        
        if (collision.CompareTag("Player"))
        {
            anim.SetTrigger("isTrigger");
            this.GetComponent<AudioSource>().PlayOneShot(grassSound);
            coll.enabled = false;

            //Spawn Items
            System.Random rnd = new System.Random();
            drop = rnd.Next(1, 31);

            if ((drop >= 1) && (drop <= 10))
            {
                Invoke("InstCoin", 0.2f);
            }
            else if ((drop >= 27) && (drop <= 30))
            {
                Invoke("InstHeart", 0.2f);
            }

            Invoke("HideSprite", 0.8f);
        }
    }
    
    private void HideSprite()
    {
        Destroy(gameObject);
    }

    private void InstCoin()
    {
        Instantiate(coin).transform.position = gameObject.transform.position;
    }
    private void InstHeart()
    {
        Instantiate(heart).transform.position = gameObject.transform.position;
    }
}

But when i hit 2 prefabs(same object) with 1 collider at the same time (cause its instant the spawn of that collider of an attack), i generate a random number that make reference to one item(Coin, Heart or nothing).
The issue comes when i get the same item in this cases from both prefabs, i think its cause the random funtion is generating one number (from some internal clock) and then uses the same number on both funtions cause are “working” at the same time but dont know…

I dont know how to solve this cause both of those prefabs are getting destroy at the same moment…

Help pls :frowning:

EDIT:
Finally got a good solution to this:

Instead make a simple random(), i used the instanceID of what i was destroying as the seed of the random:

random(prefab.getInstanceID);

That was the best solution i found

Hi!

You can create a float variable called for example “LRM” (Last Random Number). Always you generete a random number:

  1. Check if generated number is equal to “LRM”
  2. If is not, store it in LRM and use it.
  3. But if its the same, generate another number.
  4. Do 1st step again…

As you said, as rendom are generated from clock timer, the 2nd random generated can not be the same as the 1st.

If helped, Accept the answer!!

private void OnTriggerEnter2D(Collider2D collision)
{

         if (collision.CompareTag("Player"))
         {
             anim.SetTrigger("isTrigger");
             this.GetComponent<AudioSource>().PlayOneShot(grassSound);
             coll.enabled = false;
 
             //Spawn Items
             System.Random rnd = new System.Random();
             drop = rnd.Next(1, 31);
 
             if ((drop >= 1) && (drop <= 10))
             {
                 Invoke("InstCoin", 0.2f);
             }
             else if ((drop >= 27) && (drop <= 30))
             {
                 Invoke("InstHeart", 0.2f);
             }
 else{
             Invoke("HideSprite", 0.8f);
}
         }
     }

Dont know what exactly you are asking for, but might be you are missing else of hideSprite, and it is always destroying your object.