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 Armadillq · Apr 05, 2018 at 12:22 AM · randomspawnspawningrandom.range

Creating a random number of spawning items on collision

I'm trying to create a C# script that allows me to spawn a random amount of 'Money' when the 'Person' object collides with an object. I've tested some stuff but really i'm just clueless. Heres my code

The script that walks to the desk and detects collision

 public class deskWalk : MonoBehaviour
 {
     public Transform tr;
     public Rigidbody rb;
     public float speed = 2f;
     public Vector3 direction;
     public bool stopped;
     public Transform BankDesk;
     public GameObject MoneySpawner;
     
     
     // Update is called once per frame
     void Start()
     {
         int randNum;
         randNum = Random.Range(0, 10);
             Debug.Log("Moving Now");
         stopped = false;
         tr.Rotate (0, 180, 0 * Time.deltaTime);
 
     }
     private void FixedUpdate()
     {
         if (stopped)
         {
             direction = Vector3.zero;
         }
         else
         {
             rb.AddForce(tr.forward * speed * Time.deltaTime);
         }
     }
 
     void OnCollisionEnter(Collision collision)
     {
         if (collision.gameObject.transform.name == "Cube")
         {
             Debug.Log("Lol im stopped");
             stopped = true;
             MoneySpawner.GetComponent<moneySpawner>().spawn();
             
         }
     }
     public void leave()
     {
         if (stopped == true)
         {
             stopped = false;
             Debug.Log("Leaving Now!");
             tr.Rotate(0, 90, 0 * speed * Time.deltaTime);
             rb.AddForce(tr.forward * speed * Time.deltaTime);
         }
     }
 }

The script that spawns the money object

 public class moneySpawner : MonoBehaviour
 {
     public GameObject money;
     public void spawn()
     {
             moneyPooler.Instantiate(money, transform.position, Quaternion.identity);
        
     }
 
 }




Comment
Add comment · Show 1
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 losingisfun · Apr 05, 2018 at 01:11 AM 0
Share

I would recommend cache that moneySpawner script in the start method, you don't want to use GetComponent more than you have to, it can be costly on performance. e.g.

     public moneySpawner moneySpawnerScript;
     moneySpawnerScript = $$anonymous$$oneySpawner.GetComponent<moneySpawner>();

//then use moneySpawnerScript.spawn() ins$$anonymous$$d

however, if you only have one moneyspawner script in your scene, it might be worth creating a static method to spawn, so you only ever need to use 'moneySpawner.spawn()' when you want to spawn money

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by losingisfun · Apr 05, 2018 at 01:06 AM

try using a for loop and a random range, like:


 int roll = Random.Range(0,10);
 for (int I = 0; I < roll; I++) {
       //instanstiate something here
 }


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 Armadillq · Apr 05, 2018 at 01:15 AM 0
Share

Yes, finally lol

Code worked perfectly, I tend to get either 1 or 7 quite often after testing it a few times which may be really lucky or unlucky, but I think I need to tweak the code a bit to receive a more 'random' number.

Thank you very much for your help!

avatar image
0

Answer by Armadillq · Apr 05, 2018 at 01:16 AM

For anyone who would like the code that ended up working for me

 public class moneySpawner : MonoBehaviour
 {
     public GameObject money;
     public void spawn()
     {
         int randNum = Random.Range(0, 10);
         for (int I = 0; I < randNum; I++)
         {
             moneyPooler.Instantiate(money, transform.position, Quaternion.identity);
         }
     }
 
 }

Comment
Add comment · 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

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

Random Spawning of Objects at Random Locations 1 Answer

Using Vector3 in ViewportToWOrldPoint 1 Answer

Enemies spawn on top of each other(C#)(Unity) 1 Answer

How do I make a game object spawn several times in semi-random locations? 1 Answer

How to make enemy prefab spawn at random times between 1.0f to 10.0f. 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