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 cristiensen · Sep 13, 2018 at 03:22 PM · generating

Problem with randomly generating coins

Hi! I am building a 2D game in which the character keeps falling and the objective is to collect as many coins possible until the player runs out of time. When the player reaches a certain point where is a trigger, the following code is trying to generate a random number of coins on random positions:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RandomCoinPlacement : MonoBehaviour {
 
     private int randomSilver; 
     private int i;
 
     private float rndX;
     private float rndY;
 
     private Vector3 rndPos;
 
     public GameObject silver;
     //public GameObject gold;
     //public GameObject diamond;
     
     // Update is called once per frame
     void Update () {
 
         rndX = Random.Range (-16.08f, 22.32f); // random x position
 
         rndY = Random.Range (-0.21f, -23.25f); // random y position
     }
 
     void OnTriggerEnter2D(Collider2D col)
     {
         randomSilver = (int)Random.Range (0, 12); // how much silver coins will be generated for this block
         // Debug.Log (randomSilver);
 
         for (i=1; i<= randomSilver; i++)
         {
         Debug.Log (i);
         //    Debug.Log (rndX);
         //    Debug.Log (rndY);
             rndPos = new Vector3(rndX, rndY+NextAxis.TheYAxis, -0.25f);
             Instantiate (silver, rndPos, Quaternion.identity); // trying to instantiate randomSilver number of blocks
         }
 
 
     }
 }

The problem is that it generates only one coin instead of a random number and I can't figure out what I did wrong. I thought that the "for" is not called so tried Debug.Log() on "rndX" , "rndY" , "randomSilver" and "i" variables and indeed there are random numbers. Can you please help me on this one? Thanks!

PS: I am kind of new to C# and Unity.

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

1 Reply

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

Answer by I5 · Sep 13, 2018 at 03:38 PM

I think your variables "i" and "randomSilver" could be getting stepped on (unwantedly overwritten by successive collisions).

I wouldn't worry about alocs for those two variables, so don't make them member variables, that is define them each time in OnTrigger...

 //instead of 
 for(i = 0...
  
 //do
 for(int i = 0...

 //instead of 
 randomSilver = (int)Random.Range (0, 12);
 
 //do, also, no casting necessary
 int randomSilver = Random.Range (0, 12);

  

and the same for rndPos

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 cristiensen · Sep 16, 2018 at 12:22 PM 0
Share

Thank you for your answer, you helped me solve the problem. Sorry for answering late but i got caught in something.Now I'm using an array to generate random position coins and the code looks like this:

 void OnTriggerEnter2D(Collider2D col)
     {
         int randomSilver = Random.Range (0, 12); // how much silver coins will be generated for this block
         // Debug.Log (randomSilver);
 
         for (int i=0; i<= randomSilver; i++)
         {
              rndX[i] = Random.Range (-16.08f, 22.32f);
              rndY[i] = Random.Range (-0.21f, -23.25f);
 
             Vector3 rndPos = new Vector3(rndX[i], rndY[i]+NextAxis.TheYAxis, -0.25f);
             Instantiate (silver, rndPos, Quaternion.identity); // trying to instantiate randomSilver number of blocks
         }
 
 
     }

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

88 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Repeating map generation 0 Answers

FPS, character arm movement 0 Answers

Terrain generation [2D,C#]? 0 Answers

Loading level async and execute code during laod 1 Answer

Efficent generation of moving grating image 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