Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Building an incremental game...

Discussion in 'Scripting' started by Pyronide, Apr 19, 2015.

  1. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    Hey guys, so I am building an incremental game and I am still very new to coding in JS and using Unity, let alone the new version 5. Alright, what you should know to help with this is that the code doesn't seem to use the correct variable. When the player "Hires a punk" it should increase the total income by their bonus (0.02) but instead it increases the amount by the total amount of punks hired.

    Now, instead of posting the coding here as it would be a rather large amount, I am going to post a link to haste bin.

    http://hastebin.com/yemiyidewo.js

    Please also note, that as I am learning, the code may not be up to standards hahaha.

    EDIT: Forgot to mention that there isn't anything onscreen just yet so you will have to add this script to something (I am using the main camera currently) and then watch the variables in the inspector panel if you are wanting to verify the code... sorry
     
  2. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    233
    Input.GetKey > Input.GetKeyDown
    So it only runs when key is pressed, otherwise it will run 10 times while your key is held down for that split second.

    Thats really the only thing I can see wrong, try it out.
     
  3. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    Alright, so I did your change and now what happens is that it is increased by 0. I did some testing and after changing the number from 0.02 to 0.2 (so I only needed to his this button 5 times to get a whole number) it still stays at zero, then I started to debug log it... comes up "0" looked some more and saw that it was an int, changed to a float, but still "0" in the debug.log.

    I just want to clarify something... I read somewhere that an int isn't used when wanting to carry over a decimal point, is this correct? should I keep it a float?

    Also, thanks for the help
     
  4. Pyronide

    Pyronide

    Joined:
    Jun 7, 2014
    Posts:
    56
    Yes I am double posting, but it's because this was solved...

    The issue was within the constructor and the arrangement of the variables... if I put the float (_increase) first and then the int (_cost) then it works... or if I made both floats it works...

    Code Link: http: //hastebin.com/senipucapi.vala