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 Jake · Aug 06, 2010 at 06:41 PM · variablechange

Variables not changing correctly

Hello, first off sorry for asking so many questions lately. Now on with my question. My variables are changing far less than they should. Some change by one per 5 seconds, some by 0. Here is my code.

var population = 100; var food = 100.0; var happiness = 60.0; var farms = 5; var harvest = farms * 5; var hunger = population * 0.25; var starvation = 0.0; InvokeRepeating ("FoodCycle", 0, 5);

function FoodCycle () {

 var populationgrowth = Random.Range(population / 10, population / 100);
     population += populationgrowth * Time.deltaTime;        
     food += harvest * Time.deltaTime;
     food -= hunger * Time.deltaTime;

 if (food < population / 10)
    happiness -= 1 * Time.deltaTime;

 if (food < 1)
    happiness -= 5 * Time.deltaTime;

 if (1 > food)   
    starvation += 1 * Time.deltaTime;      

 if (1 < starvation)
    population -= 1;

 if (1 < starvation)
    starvation -= 1;

}

EDIT:

Now that my variables are updating at the right time there is another problem. My variables that depend on each other don't update in relation to each other. For example as population increases hunger does not. Here's my current code:

var population = 100.0; var food = 100.0; var happiness = 60.0; var farms = 5.0; var harvest = farms * 5.0; var hunger = population * 0.25; var starvation = 0.0; InvokeRepeating ("FoodCycle", 0.0, 120.0); var populationgrowth = Random.Range(population / 100.0, population / 50.0);

function FoodCycle () {

     population += populationgrowth;     
     food += harvest;
     food -= hunger;

 if (food < population / 10.0)
    happiness -= 5.0;

 if (food < 1.0)
    happiness -= 10.0;

 if (1 > food)   
    starvation += 1.0;      

 if (1 < starvation)
    population -= 1.0;

 if (1 < starvation)
    starvation -= 1.0;

 if (food < 0.0)
     food = 0.0;

 if (population < 0.0)
     population = 0.0;

 if (food < population)
     populationgrowth = 0.0;

 if (food > population * 2)
     happiness += 1.0;

 if (happiness > 100)
     happiness = 100;

 if (happiness < -100)
     happiness = -100;

}

Comment
Add comment · Show 6
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 Wolfram · Aug 07, 2010 at 12:04 AM 0
Share

Please, do ask. We are here to answer :o)

avatar image Jake · Aug 07, 2010 at 01:35 AM 0
Share

Okay, thanks! I'll ask some more!

avatar image Jake · Aug 07, 2010 at 03:06 AM 0
Share

Just updated my question.

avatar image Wolfram · Aug 07, 2010 at 12:01 PM 0
Share

Umh, but please don't add another question to already existing questions using "edit". Simply create a new question for it, or use the forum at http://forum.unity3d.com/ which is better suited for discussions.

avatar image Wolfram · Aug 07, 2010 at 12:20 PM 0
Share

If you look at your code, you make only one assignment to "hunger" - in the initialization. So it never changes, even if the population changes. Ins$$anonymous$$d, you need to update hunger accordingly. Don't write everything linearly one after the other, split your method into smaller functions. You could write a function GetHunger() which computes a new hunger value, etc. You will run into similar problems with harvest and populationgrowth.

Show more comments

2 Replies

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

Answer by Mike 3 · Aug 06, 2010 at 07:07 PM

Looks like the same issue as last time - population is an integer, so using integer/integer is a bad idea

Either make population a float, or divide by floats each time (i.e. 10.0 and 100.0)

Also - you're invoking the function every 5 seconds, but multiplying by Time.deltaTime, which is the last frame time. My guess is that you should remove all the Time.deltaTime parts, and if you must, multiply by 5 instead if you want it in value per second

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 Jake · Aug 07, 2010 at 01:34 AM 0
Share

Thanks! This solves the problem I had here. However it creates a bunch more...prepare for more questions...

avatar image
1
Best Answer

Answer by Wolfram · Aug 07, 2010 at 12:04 AM

Everything Mike said is perfectly valid. However, another problem might be your use of Random.Range(). According to the documentation, the first parameter is the minimum, and the 2nd is the maximum. population/10 is 10, population/100 is 1, so you should reverse it (and take care of floating points):

var populationgrowth = Random.Range(population / 100.0, population / 10.0);

Note the maximum is not included in the range, so this call will return values from 1 to 9 for a population of 100.

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 Eric5h5 · Aug 07, 2010 at 12:29 AM 0
Share

The maximum is included in the range, when using floats.

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

No one has followed this question yet.

Related Questions

public variable listener for dispatch in composed script 1 Answer

check if an always changing int variable suddenly stop changing 1 Answer

Change all value of variable 1 Answer

"When a value is changed..." 2 Answers

Prefab not changing variable's values 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