- Home /
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.
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
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
![](https://koobas.hobune.stream/wayback/20220612174536im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Repeating map generation 0 Answers
FPS, character arm movement 0 Answers
Terrain generation [2D,C#]? 0 Answers