Search Unity

Basic Clicker game code problem (PLEASE HELP)

Discussion in '2D' started by mongpong, Jun 2, 2017.

  1. mongpong

    mongpong

    Joined:
    Feb 16, 2017
    Posts:
    23
    I was following this tutorial and i've checked it over several times, and the script seems fine. need help!!!

    Tutorial:



    Time on video : 22:51

    please help!

    upload_2017-6-2_15-43-47.png
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, so you have an error on a line.. if you open the script and look at that line (or double click the error, it should bring you there, hopefully in the code editor).
    Then, for follow up, you can either decipher the problem yourself and/or paste the affect line into the forum here :)
     
    mongpong likes this.
  3. mongpong

    mongpong

    Joined:
    Feb 16, 2017
    Posts:
    23
    Cost - Mathf.Round(Cost * 1.15f);
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, so are you like trying to adjust the value of cost on that line?
    I know you literally posted the line, which I suggested.. but for future reference, please feel free to add a few words of your own to help explain :) lol

    If you want to adjust 'Cost', try this instead:
    Code (csharp):
    1.  Cost -= Mathf.Round(Cost * 1.15f);
    You could also do this (same thing):
    Code (csharp):
    1.  Cost = Cost - Mathf.Round(Cost * 1.15f);
    See the difference to the original code and even what the error was trying to tell you ?
     
  5. mongpong

    mongpong

    Joined:
    Feb 16, 2017
    Posts:
    23
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class NewBehaviourScript : MonoBehaviour {


    public Click click;
    public UnityEngine.UI.Text ItemInfo;
    public int Cost;
    public int count = 0;
    public int clickPower;
    public string itemName;
    private float _newCost;

    void Update()
    {
    ItemInfo.text = itemName + "\nCost: " + Cost + "\nPower: +" + clickPower;
    }

    public void PuchasedUpgrade ()
    {
    if (click.gold >= Cost)
    {
    click.gold -= Cost;
    count += 1;
    click.goldperclick += clickPower;
    Cost -= Mathf.Round(Cost * 1.15f);
    _newCost = Mathf.Pow(Cost, _newCost = Cost) ;
    }
    }

    }


    both still have errors
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay.. few things here about your post & code.
    Please use code tags when you post code in the forums. There's a pinned thread about how to do that.
    You can even edit your last post so it will be nicer.
    Anyhow, luckily it's not too long and I can read it this time ...

    You didn't add that changes I suggested.. that's 1 issue :) At least not in what you posted.

    Next, you can't use "_newCost = Cost" inside Mathf.Pow
     
    mongpong likes this.
  7. mongpong

    mongpong

    Joined:
    Feb 16, 2017
    Posts:
    23
    sorry :)
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    lol.. :)
     
  9. mongpong

    mongpong

    Joined:
    Feb 16, 2017
    Posts:
    23
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewBehaviourScript : MonoBehaviour {
    6.  
    7.  
    8.     public Click click;
    9.     public UnityEngine.UI.Text ItemInfo;
    10.     public int Cost;
    11.     public int count = 0;
    12.     public int clickPower;
    13.     public string itemName;
    14.     private float _newCost;
    15.  
    16.     void Update()
    17.     {
    18.         ItemInfo.text = itemName + "\nCost: " + Cost + "\nPower: +" + clickPower;
    19.     }
    20.  
    21.     public void PuchasedUpgrade ()
    22.     {
    23.         if (click.gold >= Cost)
    24.         {
    25.             click.gold -= Cost;
    26.             count += 1;
    27.             click.goldperclick += clickPower;
    28.             Cost = Cost - Mathf.Round(Cost * 1.15f);
    29.             _newCost = Mathf.Pow(Cost, _newCost = Cost) ;
    30.         }
    31.     }
    32.  
    33. }
    34.  
     

    Attached Files:

  10. mongpong

    mongpong

    Joined:
    Feb 16, 2017
    Posts:
    23
    still getting that error on Cost = Cost - Mathf.Round(Cost * 1.15f); but everything else is fine
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, well the error is not the same now.. Right? It's something different :)
    Add a cast:
    Code (csharp):
    1.  Cost = Cost - (int)Mathf.Round(Cost * 1.15f);
     
    mongpong likes this.
  12. mongpong

    mongpong

    Joined:
    Feb 16, 2017
    Posts:
    23
    Lets see...starting up unity now x)
     
  13. mongpong

    mongpong

    Joined:
    Feb 16, 2017
    Posts:
    23
    it seems to work! thank you!
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, you're welcome :)
     
    mongpong likes this.
  15. mongpong

    mongpong

    Joined:
    Feb 16, 2017
    Posts:
    23
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class UpgradeManager : MonoBehaviour {
    6.  
    7.  
    8.     public Click click;
    9.     public UnityEngine.UI.Text ItemInfo;
    10.     public int Cost;
    11.     public int count = 0;
    12.     public int clickPower;
    13.     public string itemName;
    14.     private float BaseCost;
    15.  
    16.     void Start()
    17.     {
    18.         BaseCost = Cost;
    19.     }
    20.    
    21.  
    22.     void Update()
    23.     {
    24.         ItemInfo.text = itemName + "\nCost: " + Cost + "\nPower: +" + clickPower;
    25.     }
    26.  
    27.     public void PuchasedUpgrade()
    28.     {
    29.         if (click.gold >= Cost)
    30.         {
    31.             click.gold -= Cost;
    32.             count += 1;
    33.             click.goldperclick += clickPower;
    34.             Cost = Mathf.Round(BaseCost * Mathf.Pow(1.15f, count)) ;
    35.         }
    36.     }
    37.  
    38. }
    39.  
     
  16. mongpong

    mongpong

    Joined:
    Feb 16, 2017
    Posts:
    23
    I'm having trouble with: Cost = Mathf.Round(BaseCost * Mathf.Pow(1.15f, count)) ;

    in the second tutorial, the guy changes the code yet again, and its not got an error on this line

    upload_2017-6-8_13-30-23.png
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That's the exact same error that I last showed you a fix for. :)
     
  18. Mitnainartinarian

    Mitnainartinarian

    Joined:
    Jun 3, 2017
    Posts:
    16
    If you want to follow the tutorial exactly (and avoid the problem showing up again), change the type of cost to a float. On your last screenshot, it's line 10, which should read:

    Code (CSharp):
    1. public float Cost;
    Or cost (without a capital c, which the tutorial uses), whatever you want, provided it's consistent throughout your code.