- Home /
Random.value not working properly?
I made a script where it spawns materials randomly, and it works, except for the fact that sometimes it doesnt work, and i got NO CLUE why. I used debug.log to figure out where the problem is, its the place in the script where i choose which material to use (in NewMaterialSpawn), works sometimes, then doesnt work, then works, doesnt work. Its really buggin me off.
void Start()
{
Diamond = false;
Ruby = false;
Iron = false;
Gold = false;
Iron1.SetActive(false);
Gold1.SetActive(false);
Diamond1.SetActive(false);
Ruby1.SetActive(false);
Iron1.GetComponent<Rigidbody2D>().gravityScale = 0;
Gold1.GetComponent<Rigidbody2D>().gravityScale = 0;
Diamond1.GetComponent<Rigidbody2D>().gravityScale = 0;
Ruby1.GetComponent<Rigidbody2D>().gravityScale = 0;
InvokeRepeating("NewMaterialSpawn", 2, 3);
}
void NewMaterialSpawn()
{
Debug.Log("YEET");
float RandomValue = Random.value;
if (RandomValue < 0.05f)
{
Ruby = true;
Debug.Log("RubyCalled");
RubyFall = true;
}
else if (RandomValue < 0.10f)
{
Diamond = true;
Debug.Log("DiamondCalled");
DiamondFall = true;
}
else if (RandomValue < 0.25f)
{
Gold = true;
Debug.Log("GoldCalled");
GoldFall = true;
}
else if (RandomValue < 0.6f)
{
Iron = true;
Debug.Log("IronCalled");
IronFall = true;
}
}
void Update()
{
if (Iron == true)
{
Debug.Log("iron");
Iron1.SetActive(true);
Iron1.transform.position = new Vector3(Random.Range(-7.2f, -4.1f), 5, 1.5f);
Iron = false;
}
if (Gold == true)
{
Debug.Log("gold");
Gold1.SetActive(true);
Gold1.transform.position = new Vector3(Random.Range(-7.2f, -4.1f), 5, 1.5f);
Gold = false;
}
if (Diamond == true)
{
Debug.Log("diamond");
Diamond1.SetActive(true);
Diamond1.transform.position = new Vector3(Random.Range(-7.5f, -3.8f), 5.1f, 1.5f);
Diamond = false;
}
if (Ruby == true)
{
Debug.Log("ruby");
Ruby1.SetActive(true);
Ruby1.transform.position = new Vector3(Random.Range(-7.2f, -3.8f), 5.1f, 1.5f);
Ruby = false;
}
}
void FixedUpdate()
{
if (IronFall == true)
{
Iron1.GetComponent<Rigidbody2D>().AddForce(transform.up * -0.1f, ForceMode2D.Impulse);
}
if (GoldFall == true)
{
Gold1.GetComponent<Rigidbody2D>().AddForce(transform.up * -0.1f, ForceMode2D.Impulse);
}
if (DiamondFall == true)
{
Diamond1.GetComponent<Rigidbody2D>().AddForce(transform.up * -0.1f, ForceMode2D.Impulse);
}
if (RubyFall == true)
{
Ruby1.GetComponent<Rigidbody2D>().AddForce(transform.up * -0.1f, ForceMode2D.Impulse);
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.name == "Iron")
{
IronFall = false;
Iron1.SetActive(false);
}
if (collision.gameObject.name == "Gold")
{
GoldFall = false;
Gold1.SetActive(false);
}
if (collision.gameObject.name == "Diamond")
{
DiamondFall = false;
Diamond1.SetActive(false);
}
if (collision.gameObject.name == "Ruby")
{
RubyFall = false;
Ruby1.SetActive(false);
}
}
}
Answer by KoenigX3 · May 30, 2020 at 03:48 PM
For correct randomization, you need to use the cumulative percentages. If you have 5%, 10%, 25% and 60%, then you need to add the previous percentages and chech against those. These are in this case: 0.05f, 0.15f, 0.40f, 1.00f. I guess that's why you were trying to use Random.value. With this solution, there is still a problem: Random.value can output both 0 and 1. In this situation, you either only check with else at the last part, or you can use Random.Range(0, 100) if the percentages are whole numbers, Random.Range(0, 1000) if they have 1 decimal, etc.
Another thing, you don't need to check for those booleans to see if you need to perform an action. You can call all of them on NewMaterialSpawn.
void NewMaterialSpawn()
{
if(value < 0.05f)
{
Ruby1.SetActive(true);
// Change Ruby1 tranform position here
}
// Same for the others
}
Also, if you have gravity enabled, those objects will fall down without adding force to them. You can even modify the gravity constant to speed it up. Plus, using GetComponent on each FixedUpdate is not a good practice. Get them on Start and store them in a variable to use it later.
RigidBody rubyBody;
void Start()
{
rubyBody = Ruby1.GetComponent<RigidBody2D>();
// Now you can later use rubyBody.AddForce();
}
EDIT: I've just realized that you have set gravity to 0 for those objects on purpose.
Thank you so much. I changed it to:
void New$$anonymous$$aterialSpawn() { float RandomValue = Random.Range(0, 100); if (RandomValue < 5) { Ruby = true; RubyFall = true; } else if (RandomValue < 15) { Diamond = true; DiamondFall = true; } else if (RandomValue < 40) { Gold = true; GoldFall = true; } else if (RandomValue < 100) { Iron = true; IronFall = true; } }
and it worked perfectly
EDIT: seems like i cant paste scripts in the reply thing xd
I'm glad it helped. Two more things: you can use int ins$$anonymous$$d of float, and you don't need to check for RandomValue < 100, you can use a simple else statement. The reason is that the first 3 if-else blocks will cover all the numbers from 0-39, so when you get to the 4th, you will only have matching numbers.
Your answer
Follow this Question
Related Questions
probability of child Game objects 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Spawn chance in my spawner. 2 Answers
Random movement script not working C# 2 Answers