Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 floppyturtle32 · Dec 07, 2020 at 03:08 AM · error3ddebug.log

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.

Comment
Add comment · Show 3
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 SmallLion · Dec 07, 2020 at 01:07 PM 0
Share

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.

avatar image floppyturtle32 · Dec 07, 2020 at 02:05 PM 0
Share

Thanks for your answer, I will make sure to try it out.

avatar image floppyturtle32 floppyturtle32 · Dec 07, 2020 at 02:38 PM 0
Share

Alas, the most reliable trick in the book has failed me.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · 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 floppyturtle32 · Dec 07, 2020 at 04:14 AM 0
Share

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.

avatar image sacredgeometry floppyturtle32 · Dec 07, 2020 at 12:00 PM 0
Share

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?

avatar image floppyturtle32 sacredgeometry · Dec 07, 2020 at 02:03 PM 0
Share

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.

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

232 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 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 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

Unity Projects 01 Stealth - problem! 1 Answer

Raycast does not go in the right direction,Raycast not pointing in the correct direction 0 Answers

Image effect Shader giving weird error. 0 Answers

How do i fix Error CS0029? 1 Answer

Really weird Unity Camera Bug (In Editor) 2 Answers


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