- Home /
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 :(
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
Answer by tormentoarmagedoom · Feb 21, 2018 at 12:21 PM
Hi!
You can create a float variable called for example "LRM" (Last Random Number). Always you generete a random number:
Check if generated number is equal to "LRM"
If is not, store it in LRM and use it.
But if its the same, generate another number.
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!!
Its not a bad solution, but if i do it like you said, if i destroy both prefabs its impossible the posibility of get the same objects from both prefabs. And other thing that i dont know how works is... 2 instances of the same method in the same time trying to modify the same variable? It makes some threading o how works unity with that? :|
Answer by upasnavig90 · Feb 21, 2018 at 12:33 PM
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.
I want to destroy a bush and get some item with some system of randoms simple as that