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 /
  • Help Room /
avatar image
0
Question by MorganLee909 · Oct 01, 2015 at 08:31 AM · variablesif-statementsconditional

If statement unexpectedly changing value.

The update method is supposed to create a zombie for my game every 10 seconds. I just tried to add a random element to it so that it doesn't spawn every time, just randomly. The problem is that the random variable that I created is being changed to less than or equal to one, making it not random at all.

I don't understand why this is happening, I have code just like this all throughout my program and it works just find everywhere except for here. I understand that for some reason it isn't being read as conditional but I have no idea why, it has always worked for me. What am I doing wrong? I am sure that it must be something simple.

I added the whole Update method for reference, but the part that I am talking about is the first part of the if statement, the other stuff doesn't really matter. The variable of random is declared just above it.

 void Update () {
     float random = Random.Range(0f, numberOfSpawners);
     int variable = Mathf.RoundToInt (Time.realtimeSinceStartup % spawnRate);
     if (random <=1 && variable == 0f && !alreadySpawned) {
         LevelControl();
         spawnCount++;
         spawnRate -= .1f;
         GameObject spawnedZombie = Instantiate(zombie, transform.position, Quaternion.identity) as GameObject;
         spawnedZombie.transform.parent = gameObject.transform;
         alreadySpawned = true;
     } 
     else if (variable != 0f  && alreadySpawned){
         alreadySpawned = false;
     }
 }
 



Comment
Add comment · Show 2
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 hexagonius · Oct 01, 2015 at 09:44 AM 0
Share

debug numberofspawners. I think it's one

avatar image MorganLee909 hexagonius · Oct 02, 2015 at 03:40 AM 0
Share

No, it is set to 4 at the moment. What is happening is if I print random before the if statement then it gives me a random number in the range selected. However, if I print random inside the if statement then it gives me between 0 and 1. The if statement is changing the value of random to be less than or equal to one.

0 Replies

· Add your reply
  • Sort: 

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

30 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

Related Questions

The first if statement works and "pads" move but it stops at position 1, why isn't working ? (pads is of type Vector3) 0 Answers

Problem while using several tags in a OnTriggerEnter (Collider other) 0 Answers

Referencing variables in another script on the same game object 0 Answers

Shaders - Avoiding Branches. Can 'true / false' be instanced as '1 / 0'? 1 Answer

Unity quest system a better way? 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