Search Unity

Question Can't call a method within an if statement

Discussion in 'Scripting' started by piahe, Jun 27, 2021.

  1. piahe

    piahe

    Joined:
    Apr 17, 2019
    Posts:
    3
    Hey,
    I have the following problem:
    My Code looks like this and I'm using the pedometer api (https://github.com/natsuite/NatStep).
    If I want to call my "progressbar" from the position where I commented it out, it doesn't work.
    It's only working if I call it directly after "stepText.text = loadSteps.ToString();", but the main problem is that "distanceMeterText.text = loadDistance.ToString();" and the other one would not work, when I try to call the method.
    The script works fine without the method.

    code where i want to call the method:

    Code (CSharp):
    1. public void OnStep(int steps, double distance)
    2.         {
    3.             // Display the values    
    4.             //stepText.text=steps;
    5.  
    6.             //adding the new Steps to the old steps
    7.  
    8.             float distancef = (float)distance;
    9.            
    10.            
    11.             if (loadSteps == steps)
    12.             {
    13.  
    14.                 stepText.text = loadSteps.ToString();
    15.  
    16.                 distanceMeterText.text = loadDistance.ToString();
    17.  
    18.                 float loadKM = loadDistance / 1000;
    19.                 distanceKMText.text = loadKM.ToString();
    20.  
    21.                 //progressbar
    22.                 //float adderSf = loadSteps;
    23.                 //float stepgoalf = stepgoal;
    24.                 //circlebar(adderSf, stepgoalf);
    25.  
    26.             }
    27.             else
    28.             {
    29.                 adderS = loadSteps + steps;
    30.  
    31.                 stepText.text = adderS.ToString();
    32.                 SaveVarInt(adderS);
    33.  
    34.                    
    35.                 float adderD = loadDistance + distancef;
    36.                 distanceMeterText.text = adderD.ToString();
    37.                 SaveVarFloat(adderD);
    38.  
    39.                 float adderKM = adderD / 1000;
    40.                 distanceKMText.text = adderKM.ToString();
    41.  
    42.                 //progressbar
    43.                 //float adderSf = adderS;
    44.                 //float stepgoalf = stepgoal;
    45.                 //circlebar(adderSf, stepgoalf);
    46.             }
    47.      }
    circle progress bar code
    Code (CSharp):
    1. private void circlebar(float adderSf, float stepgoalf)
    2.         {
    3.             float percentage = adderSf / stepgoalf;
    4.  
    5.             if (percentage > 0.999)
    6.             {
    7.                 percentage = 1.0f;
    8.             }
    9.             CircleProgressBar.GetComponent<Image>().fillAmount = percentage;
    10.         }
    just for summary:

    if i call the method where it's now stated, it wouldnt work.
    if i call the method between steptext and distancetext, distancetext and the following would not work.

    Maybe someone can help me. I'm sorry for my broken english.
     
  2. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Add debug.log statements everywhere.

    For example (this is just an example, don't forget to add Debug.Log statements elsewhere too):

    Code (CSharp):
    1.             if (loadSteps == steps)
    2.             {
    3.                 Debug.Log("Setting stepText");
    4.                 stepText.text = loadSteps.ToString();
    5.                 Debug.Log("Setting distanceMeterText");
    6.                 distanceMeterText.text = loadDistance.ToString();
    7.                 Debug.Log("Setting distanceKMText");
    8.                 float loadKM = loadDistance / 1000;
    9.                 distanceKMText.text = loadKM.ToString();
    10.                 Debug.Log("doing progressbar");
    11.                 //progressbar
    12.                 //float adderSf = loadSteps;
    13.                 //float stepgoalf = stepgoal;
    14.                 //circlebar(adderSf, stepgoalf);
    15.             }
    Then check the log to see where in the function it stops executing. Check for exceptions.
     
    piahe likes this.
  3. piahe

    piahe

    Joined:
    Apr 17, 2019
    Posts:
    3
    Thank you very much, it helped alot.
    My Problem was a null reference exception, because i didnt have all the objects i needed in every scene declared. so i ended up having some invisible gameobjects in my scenes, but it works now without any problems.