Search Unity

Float variable A = float variable A * float variable B ??

Discussion in 'Scripting' started by alex3b42, Aug 20, 2019.

  1. alex3b42

    alex3b42

    Joined:
    Apr 19, 2018
    Posts:
    2
    Hi,
    so I have population and population growth in my game, the population should be multiplied by the population growth, and idk how to do this.
    I thought it should be smt like this:
    population = (population * population_growth); but this doesn't work

    Can somebody help me with this issue?

    Thank you in advance!
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    the math you show would make the population smaller or larger (depending if the population_growth is bigger or smaller than 1).

    If "it doesn't work", maybe you don't run the code. Or you don't visualize the population variable... if you need more advise, show more code and describe better what you mean with "doesn't work".
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    If you are adding on to the existing population, you need to add that on...

    population += (population * population_growth);
     
    alex3b42 likes this.
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I mean, it does work. That's exactly how you do that. There must be something else in context that would be making it not work. (Also, please define what you mean by "doesn't work" - the number doesn't change? Compiler error? Runtime error?)
     
    lordofduct likes this.
  5. alex3b42

    alex3b42

    Joined:
    Apr 19, 2018
    Posts:
    2
    Imma F***ing retarded now, I firstly forgot to add the + to the population += (population * population_growth); and secondly I did put it into the wrong section of the code, in start and not into the timer I added.
    Still thanks for the help :)