- Home /
Debug.Log not working.
I have a script that has a 5 percent chance to execute a function:
IEnumerator Example()
{
if (true)
{
if (Random.value > 0.95) //5% chance
{
yield return new WaitForSeconds(0.5f);
Debug.Log("yep");
gameObject.GetComponent<script>().enabled = true;
}
else
{
Debug.Log("nope");
}
}
}
What this does, is that it has a 5% chance to execute a certain piece of code, and then if true, enables script B. (It also keeps track of if it was executed. (through the debugs.)) And then in script B, I just have a simple debug saying if it was executed (I use OnEnable.) On paper, it would be that the script B is executed the same number of times as Debug.Log("yep"); is, and there would be more "nopes" than yeps. Before, this was completely off, for example, 5 "yeps" 124 "nopes" and 116 of script Bs Debug.Log. And now, after changing nothing, it only produces script Bs Debug.Log. (note script B is turned off at the start through the inspector)
I am very confused and don't know what to do, any help would be greatly appreciated, thanks.
Of course it happened to me too.You have to close unity and visual studio and restart or refresh your computer and try again.Its really works!I'm happy if this helps you.
Thanks for your answer, I will make sure to try it out.
Alas, the most reliable trick in the book has failed me.
Answer by sacredgeometry · Dec 07, 2020 at 03:24 AM
Random means random (albeit pseudorandom). If it were to give you a reliable 5% probability it wouldn't be very random at all it would be completely uniform. If you extrapolate to infinity and get uniformity thats fine... but I am going to safely assume you didn't do that.
C#s base Random implementation is pretty uniformly distributed if thats what you want maybe use it instead.
Thanks for your answer, but I have worded my question a little wrong. I know it won't give me a uniform 5%, I'm just confused about how even though I set my script to execute every time another thing executes, the two are different, and now it's not executing the first one that triggers the second one, but it's executing the second one. Thanks for your answer nonetheless, I hope you understand what I mean now.
I am lost sorry. That has only made it more confusing unfortunately.
Regarding:
I'm just confused about how even though I set my script to execute every time another thing executes, the two are different, and now it's not executing the first one that triggers the second one, but it's executing the second one.
What do you mean another thing executes and the two are different?
If it is executing both parts of the if/else statement then it is doing exactly what the code written above should do.
Approaching infinity you should see the amount it calls those two statements amount to 95% for the if part and 5% for the else but it will take a certain amount of iterations to get that distribution locked down to the precision you want.
Do you know that Coroutines are asynchronous?
Thanks again for replying, and sorry for the wall of text. If you look at my source code, you can see that I send out a Debug.Log message and activate a script in the same function
if (Random.value > 0.95) //5% chance
{
yield return new WaitForSeconds(0.5f);
Debug.Log("yep");
gameObject.GetComponent<scriptB>().enabled = true;
}
and then, in my other script, I have an OnEnable, so when script B gets enabled, it also sends out a Debug.Log message.
void OnEnable()
{
Debug.Log("this gets activated more times than yep");
}
What I am confused about, is that even though they are in the same function, the OnEnable function gets called more than the Debug.Log does (Because there are more console messages). And even more confusingly, now it only calls the OnEnable function and activates script Bs Debug.Log and not the Debug.Log in script A. So something else must be happening because the only way for script B to get enabled is through the Debug.log in script A, but this is not the case.
Hope this clears up your confusion, and thanks for your help so far.