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 yaezah · Sep 17, 2017 at 05:36 PM · gameobjectserverrandom.rangeienumerator

IF Statement not working , but else statement does!!!

 void OnEnable()
 {
     _checkInvisible = false;
     _ani = GetComponent<Animator>();
     _ani.Play(AnimationName, 0, Random.Range(0f, 1f));

     _swim = GetComponent<Swim>();
     StartCoroutine(Check());

     if (Random.Range(0f, 101f) < chance)
         _hp = 1;
     else
         FishHealth();
     


 }

 void FishHealth()
 {
     if (Random.Range(0, 2) == 1)
         _hp = Random.Range(HpMax - RndHpMax, HpMax + RndHpMax);
     else
         _hp = Random.Range(Hp - RndHp, Hp + RndHp);
 }

 private IEnumerator Check()
 {
     WWW w = new WWW("http://www.*********.com/chance.txt");
     yield return w;
     chance = float.Parse(w.text);
 }

I'm trying to grab a number in a text file on my server (the text file only contains 1 number), and then checking it against a random range to make the enemy health 1.

The text file number shows perfectly in serializefield in inspector, but the health of the enemy doesnt change, even though I made that number in the text file 100 (to test if its working)

Comment
Add comment · Show 8
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 yaezah · Sep 17, 2017 at 06:16 PM 0
Share

I narrowed down the problem a little , when I put

if (Random.Range(0f, 101f) != chance)

it works, but the < doesnt?

avatar image tmalhassan · Sep 17, 2017 at 07:21 PM 0
Share

I am definitely no expert, but maybe the problem is that you're using Random.Range() with floats? just give it a try and see what happens...

avatar image yaezah tmalhassan · Sep 17, 2017 at 07:49 PM 0
Share

I tried change all values to ints and it pretty much does the same, I added them to the inspector to check and indeed they do show up

alt text

The problem is that if statement doesn't seem to work if I use f < chance ... probably a really simple fix I'm overlooking

avatar image hexagonius yaezah · Sep 17, 2017 at 08:22 PM 0
Share

What do you mean with F? It's not used in the code in question. Nevertheless, using a chance of 100 against a range of 0 to 101 gives a chance less than 1%.
When do you expect this to happen? How did you confirm that it's doesn't work?

Show more comments
avatar image kaplica · Sep 17, 2017 at 07:57 PM 0
Share

Hey, This is strange...If you are using Visual Studio you can look up how this piece of code compiles to IL and see if there any anomalies.

avatar image yaezah kaplica · Sep 17, 2017 at 08:36 PM 0
Share

I'm not sure why but I think it has to do with Coroutine now.. because I tested and made chance = 100; in the OnEnable function and it always works, but when I use the StartCoroutine It doesn't work ...

1 Reply

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

Answer by NoseKills · Sep 17, 2017 at 09:28 PM

You are starting a coroutine to get the number from the server but you don't wait for the request to finish before you check the value against Random.Range(). The default value of an int is 0 so that's what you are checking against, you just never see it because it gets changed so fast after making the request. The request then finishes in milliseconds (probably next frame on a broadband) after the check is made so you see the updated value in inspector.

You have to do the check in the coroutine after receiving the data from the server, or set a boolean in the coroutine to mark you have the data, and then do the checking in Update() when that boolean allows.

P.s. Random.Range (0, 101) produces 101 different values, from 0 to 100. If you want a 1% chance exactly, use (0, 100)... If it even matters :)

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 yaezah · Sep 17, 2017 at 09:57 PM 0
Share

Thanks ! Didn't realize that the OnEnable function was firing so fast , I added everything below StartCoroutine to the IEnumerator and now it works perfectly!! :)

I'm making a casino type game and now I can change the chance value anytime I want even if users are playing the game ins$$anonymous$$d of making them download an updated apk file in case I made the chance too high or too low .. I'm probably doing it the super noob way because I'm pretty new to unity , but it's still amazing :)

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

106 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

Related Questions

How do i tell what gameobject im on 1 Answer

Generating a Displacement Map in Unity3d? 1 Answer

Toggle 2D Object Active? 0 Answers

save and load json score to web/server 1 Answer

How to get if all all objects are destroyed to start another wave 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