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. Dismiss Notice

Have i lost my mind, simple script nor working

Discussion in 'Scripting' started by melonhead, Apr 2, 2022.

  1. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    why is this simple if stack not working, it is not executing past 1, i tried if else also and still not working, the debug is only showing 0 in the console, never 1,2 etc even though the counter is increasing?

    is it just me?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class debugtest : MonoBehaviour {
    7.  
    8.  
    9.  
    10.  
    11.     public float counter;
    12.    
    13.    
    14.     void Update ()
    15.     {
    16.  
    17.  
    18.  
    19.         if (Input.GetKey(KeyCode.Mouse0)){
    20.  
    21.                         counter=counter+0.02f;
    22.  
    23.          }
    24.  
    25.  
    26.  
    27.  
    28.  
    29.  
    30.         if (counter<1.0f)
    31.         {
    32.  
    33.             Debug.Log("0");
    34.  
    35.  
    36.             if (counter>1.0f)
    37.             {
    38.        
    39.                 Debug.Log("1");
    40.             }
    41.    
    42.  
    43.             if (counter>2.0f)
    44.             {
    45.    
    46.                 Debug.Log("2");
    47.             }
    48.    
    49.  
    50.             if (counter>3.0f)
    51.             {
    52.  
    53.                 Debug.Log("3");
    54.             }
    55.  
    56.         }
    57.     }
    58. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    You've got SO MANY Debug.Log() statements in there and yet you have failed to print the ACTUAL quantity you care about!!

    Why not print the value of
    counter
    each frame!? It would instantly reveal what is happening. Instantly.
     
  3. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    what do you mean, the counter value is a public variable displayed in the inspector of the script while it is running, so i can see that the counter value is increasing,, how do i have SO many debugs, 1 per value?
     
  4. khrysller

    khrysller

    Joined:
    Mar 14, 2019
    Posts:
    125
    And the value on the inspector is showing the way it should?
     
  5. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Try a technique called Rubber Duck Debugging. All you do is speak it out through your head, what is the code going to do?

    Lets try it.

    So if we are pressing Mouse0 this frame then we are increasing the counter by 0.02, then, we check if the counter is less than 1.0, if it is then we are printing a debug, once we have figured out that the counter is less than 1 then we check, within the same if statement, if the same counter value is greater than 1, 2, or 3.

    Now just read that and tell me what's wrong.
     
    Bunny83 likes this.
  6. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    just noticed no end brace on the first if statement/ what an idiot!!!
     
  7. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Are you sure about that?
     
  8. melonhead

    melonhead

    Joined:
    Jun 3, 2014
    Posts:
    603
    my bad
     
    Bunny83 likes this.
  9. Omniglitch

    Omniglitch

    Joined:
    May 29, 2017
    Posts:
    37
    I also noticed the value of counter is never initialized, which could lead to all kinds of random and buggy results. You can initialize it as you declare it, like this...
    Code (CSharp):
    1. public float counter = 0f;
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Not correct. C# does not allow undefined variables. That variable is a class instance variable and is therefore defined to be initialized to
    default(T)
    when the class is constructed.

    For a
    float
    this means it will be
    0.0f
    . This is enforced by the language.

    Be careful with field initializers! Because the variable is public / serialized, field initializers can actually lead to more confusing results in Unity. Here's why:

    Field initializers versus using Reset() function and Unity serialization:

    https://forum.unity.com/threads/sensitivity-in-my-mouselook-script.1061612/#post-6858908

    https://forum.unity.com/threads/crouch-speed-is-faster-than-movement-speed.1132054/#post-7274596

    Serialized properties in Unity are initialized as a cascade of possible values:

    - what the class constructor makes (field initializers or else default(T) )

    - what is saved with the prefab

    - what is saved with the prefab override(s)/variant(s)

    - what is saved in the scene and not applied to the prefab

    - what is changed in Awake(), Start(), or even later etc.

    So you want to make sure you only initialize things at ONE of the above levels, or if necessary, at levels that you specifically understand in your use case. Otherwise errors will seem very mysterious.
     
    MelvMay and Bunny83 like this.
  11. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,528
    Right, that's one of the great things about C# compared to C++ or C :) It's really hard to shoot yourself in the foot. It's still possible, but not that easy and in most cases would involve unsafe code and doing kinda stupid things.
     
    Kurt-Dekker likes this.