Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ImAntizero · Feb 21, 2018 at 12:14 PM · randomprefabscollider2dspawning

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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:

  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!!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ImAntizero · Feb 21, 2018 at 12:35 PM 0
Share

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? :|

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ImAntizero · Feb 21, 2018 at 12:39 PM 0
Share

I want to destroy a bush and get some item with some system of randoms simple as that

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

83 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawning random questions at a time 1 Answer

Transform.position without Vector3? 1 Answer

Different way to spawn prefabs 0 Answers

Prevent collision between prefab clones on a 2d mode? 0 Answers

Spawn help, 4 spawners but need one object on screen at a time 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges