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 sam32x · Apr 15, 2013 at 12:43 AM · gameobjectvariableupdatesomethingdivision

1 divided by 2 = 0?

ok so as soon as i press "p" to increase the difficulty, difficulty increases to 2 and the timer sets to 0. i already tried making it a float but that didn't change anything?

 var xPos : float = Random.Range(-50,50);
 var yPos : float = Random.Range(-50,50);
 var zPos : float = Random.Range(69,9001);
 var spawnPos : Transform;
 var spawnObject : GameObject;
 var timer : float = 1;
 var difficulty = 1;
 var score = 0;
 var countScore = 1;
 
 function Start () {
 Invoke("CustomUpdate",timer);
 }
 
 function CustomUpdate () {
 Invoke("CustomUpdate",timer);
 xPos = Random.Range(-50,50);
 yPos = Random.Range(-50,50);
 zPos = Random.Range(69,9001);
 spawnPos.position.x = xPos;
 spawnPos.position.y = yPos;
 spawnPos.position.z = zPos;
 Instantiate(spawnObject,spawnPos.position,spawnPos.rotation);
 if(countScore == 1){
 score += 1;
 }
 }
 
 function Update () {
 if(Input.GetKeyDown("o")){
 if(difficulty >= 2){
 difficulty -= 1;
 }
 }
 if(Input.GetKeyDown("p")){
 if(difficulty <= 9){
 difficulty += 1;
 }
 }
 timer = 1 / difficulty;
 }
 
 function Message () {
 countScore = 0;
 }
 
 function OnGUI () {
 GUI.Label (Rect (10, 10, 200, 40),"score:" + score);
 GUI.Label (Rect (10, 60, 200, 40),"difficulty" + difficulty);
 }
Comment
Add comment · Show 1
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 Zarkow · Jun 16, 2018 at 03:07 PM 0
Share

Don't declare fields as 'var', limited that for being snazy inside functions. It will help you avoid doing logical mistakes.

Now you have to 'guess' (since you seem unsure) what type of the result '1 / difficulty' will be. It is a int div by an int, the result will be an int. Hence, 0.

3 Replies

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

Answer by Chronos-L · Apr 15, 2013 at 12:48 AM

if you are doing division with int, the result will be round down, so 1/2 is 0.5 and it will be round down to 0.

To get the 0.5:

 var timer : float = 1; //You got this right    
 ...
 //This will do, I think
    //Embarassing mistake
    timer = ( 1 * 1.0f ) / difficulty;

    //A better correction
    timer = 1.0f / difficulty;
Comment
Add comment · Show 4 · 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 Unitraxx · Apr 15, 2013 at 12:56 AM 1
Share

Ha make it

 timer = 1.0f / difficulty;

:D

avatar image Chronos-L · Apr 15, 2013 at 01:29 AM 0
Share

This is very embarassing, I was thinking in:

 floatNumber = intVariable / intVariable;

so I make the correction to be:

 floatNumber = ( intVariable * 1.0f ) / intVariable;
avatar image instruct9r · Apr 15, 2013 at 11:16 AM 0
Share

I am wondering, why do you have to write (1.0f) when you can just do it (1.0) and Unity will still know, that it's a float value? Does that "f" helps Unity to calculate it faster or something?

avatar image Chronos-L · Apr 15, 2013 at 11:24 AM 2
Share

In UnityScript 1.0 is considered as float while in C# it is a double. So in C#, we need to type 1.0f to make it a float. Whether you write 1.0 or 1.0f doesn't matter in UnityScript.

avatar image
1

Answer by instruct9r · Apr 15, 2013 at 12:57 AM

you just have to make the 1 a float:

timer = 1.0 / difficulty;

Comment
Add comment · 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
1

Answer by Rajminster · Apr 15, 2013 at 02:22 AM

First off you never specified difficulty as an float value. Since it had no decimal Javascript automatically makes it a Integer. With Integer values and division what happens is something called "truncating".

Sure, it knows that 1/2 = 0.5 but that isn't a Integer value. Ints are only concerned with the left side of the decimal. So the result is that the .5 is dropped and the 0 is returned.

Both solutions above are perfectly fine, but I also wanted to elaborate why this occurs. Be careful, this happens for all mathematical operations for ints, and ints have range of ~-2 billion something to ~2 billion something.

Comment
Add comment · 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

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

15 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

Related Questions

how can I display a variable as a GUIText 5 Answers

Trigger LateUpdate on inactive gameObjects 2 Answers

Assigning current color to a variable for fade out (C#) 0 Answers

My variable doesn't update real-time? 2 Answers

How to create gameobject with different setting 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