Search Unity

best way to check if requirements are met?

Discussion in 'Getting Started' started by Sylvir, Mar 31, 2015.

  1. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    I am making a clicker game, and i have this code as my idea to check if i have 50 tech points i can purchase a virus.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TPB : MonoBehaviour {
    5.  
    6.     public UnityEngine.UI.Text TPC;
    7.     public UnityEngine.UI.Text TechPoints;
    8.     public int techPoints  = 0;
    9.     public int techPointsPerClick = 1;
    10.  
    11.    
    12.    
    13.     void Update(){
    14.         TechPoints.text = "Tech Points: " + techPoints;
    15.         TPC.text = "Tech Points Per Click: " + techPointsPerClick;
    16.        
    17.     }
    18.    
    19.     public void Clicked(){
    20.         techPoints += techPointsPerClick;
    21.        
    22.     }
    23.    
    24. }
    25.  
    26.  
    27.  
    28.  
    29. and the other is this..
    30.  
    31.  
    32. using UnityEngine;
    33. using System.Collections;
    34.  
    35. public class VirusClick : TPB  {
    36.  
    37.     public int VirusCost ;
    38.     public VirusClick click;
    39.     public UnityEngine.UI.Text vpc;
    40.     public UnityEngine.UI.Text VirusCount;
    41.     public float virus  = 0.00f;
    42.     public int virusPerClick = 1;
    43.  
    44.  
    45.  
    46.  
    47.     void update(){
    48.         VirusCount.text = "Virus Count: " + virus;
    49.         vpc.text = "VPC: " + virusPerClick;
    50.  
    51.     }
    52.  
    53.  
    54.  
    55.     public void Clicked(){
    56.         VirusCost = 50;
    57.             if(techPoints >= VirusCost) {
    58.             techPoints -= VirusCost;
    59.             virus += 1;
    60.             }
    61.  
    62.            
    63.         }
    64.  
    65.    
    66.     }
    67.  
    68.  
    for some reason it is not allowing me to purchase any viruses even when i reach 50 or more tech points, is there a better way to set this kind of a code up, would a bool work better? also, if this is a good way to do it, do you have any idea where i am messing up at? thanks
     
  2. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    See what techPoints is before and after like this:

    Code (CSharp):
    1. public void Clicked(){
    2.         VirusCost = 50;
    3.           Debug.Log("techPoints before:"+techPoints);
    4.             if(techPoints >= VirusCost) {
    5.             techPoints -= VirusCost;
    6.             virus += 1;
    7.             }
    8.           Debug.Log("techPoints after:"+techPoints);
    9.          
    10.         }
     
  3. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    oh thanks, good idea. i all ways forget about the debug statements..
     
  4. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    i am getting these errors i just noticed..

    NullReferenceException: Object reference not set to an instance of an object
    VirusClick.Update () (at Assets/Scripts/VirusClick.cs:18)


    and

    NullReferenceException: Object reference not set to an instance of an object
    TPB.Update () (at Assets/Scripts/TPB.cs:15)

    not quiet sure what that means, but it sounds like i didnt set a script to an object in the scene before running it? could this be why the virus's cant be bought ?

    if i take out the if statement and just leave it it lets you click for viruses and tech points, its just when i try and run a check that i have an issue
     
  5. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    those errors are fixed now, and the debug says 0 before and 0 after, i wonder why its not assigning them..
     
  6. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    It's telling you look at line 18 of VirusClick.cs and to look at line 15 of TPB.cs ... the problems are with those lines.

    My guess is that you do not have those "text" items attached to a Game Object in the hierchy. So you are trying to display a text value but there is no object to actually display it.
     
  7. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    yeah, i fixed those, and the debug it saying before and after its 0, so for some reason its not assigning the tech points even though its showing in the edditor to go up when i click, and on the text. any idea what i did wrong in assigning them?
     
  8. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    Nothing jumps out at me as to why it's not working... try lots more Debug statements EVERYWHERE... Debug is your friend!

    And BTW void update() should be void Update()
     
  9. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    Thank you for taking a look at it, i am pretty puzzled as to why its not working.
    Oh, and i got that fixed after i posted, but thanks for letting me know! ill try some more debug..

    the inheritance i used that way should let the techPoints variable from the one script go over to the virus one right? do i need to have that as monodevelop instead?
     
  10. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    it showing them adding for every click with debug logs, hmm..
     
  11. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    its looking like they are getting added in the first script, but the 2nd one where the virus code is written it is not finding them, its just seeing it as a blank empty variable.
     
  12. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    It's probably something pretty simple that I am not seeing (like you said, virus script is not seeing techPoints in the other script for some reason). I am a beginner/intermediate programmer but new to Unity and C#. Sorry I can't help more than that.
     
  13. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    okay, well thank you for the help, and time. i am new as well, that is why im confused. ill keep looking around and reading some more documentation. Just seems like it should be set up to see it, even says it does in the code.
     
  14. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    its showing after click it adds 1, but then instantly goes back to 0.
     
  15. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    just figured it out, i need the techPoints variable to be static in the first script so that it can save the number.
     
  16. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220
    The other thing you could try is to leave the variable NOT static, but do not set it to 0 either when you declare it. Set it to 0 in Start().

    So do this for the declaration:
    public int techPoints;

    Then add this:
    Code (CSharp):
    1. void Start() {
    2.     techPoints  = 0;
    3. }
     
  17. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    oh ok cool thanks, ill remember that too.