Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

int gets initialized with the value 0 no matter what

Discussion in 'Scripting' started by denissuu, Feb 21, 2020.

  1. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    112
    Hi there! So... I am trying to make a health bar that depletes by 20 every time I get hit.Before merging the code inside my playercontrols script, the issue was that I couldn't manage to call one of the functions.
    But now, after merging them in the same script, I don't know what happens but my health always gets initialized as being 0.Debug.Log-ing it in the start function after it was declared doesn't display anything at all.

    Here is the code :
    Code (CSharp):
    1.    
    2. public const int Health_max = 100;
    3.     private int health;
    4.     public Image HealthBar;
    5.  
    6.     private void Start()
    7.     {
    8.         player = GetComponent<Rigidbody2D>();
    9.         boxcoll = GetComponent<BoxCollider2D>();
    10.         health = Health_max;
    11.     }
    12.  
    13.  
    14. public int GetHealth()
    15.     {
    16.         return health;
    17.     }
    18.  
    19.  
    20.     public void OnDamageReceived()
    21.     {
    22.         GetHealth();
    23.         health -= 20;
    24.         UpdateHealth();
    25.  
    26.     }
    27.  
    28.     private void Awake()
    29.     {
    30.         HealthBar = transform.Find("HealthBar").Find("GreenHP").GetComponent<Image>();
    31.     }
    32.  
    33.  
    34.     public void UpdateHealth()
    35.     {
    36.         HealthBar.fillAmount = (float)health / Health_max;
    37.     }
    38.  
    39.  

    Can someone help me fix this code? I've been trying to for at least three hours at this point.I don't see anything wrong at all in here.Anything else other than this works perfectly fine.

    Thanks for the help! You guys rock. :)
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,232
    Debug.log should display something in Start if it's being run. Make sure your console has all parts turned on so you can see any print outs.

    Otherwise, I'm not seeing any issues with your code. How do you know you're getting a value of 0 if you aren't seeing any debug messages?

    Is your script active? Is the GameObject it's on active?
     
    denissuu likes this.
  3. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    112

    Oh well, I know that because I use the debug view in the inspector.Usual Debug.Log's with text get displayed and components get requested.So it seems that stuff also works there.I just can't get the health values in there at all.
    It is active, and it is on the proper GameObject.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,232
    I'm not 100% I understood your response, however if you put

    Code (CSharp):
    1. Debug.Log("Health is: " + health);
    in the Start method and it doesn't show in the console when you enter play mode(if you have all messages turned on), your script is not executing, which is also why you wouldn't see health get set to 100.
     
    denissuu likes this.
  5. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    112

    I'll do that when I get back home.And also, the rest of the stuff goes fine.My image's fill goes down based on the health missing, my damage function works on a button and all of that.
    I'll get back to you with an update when I'll have the opportunity to test this out.
     
  6. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    112

    Well, I did put that in the start method after health gets the value of health_max, and my Debug.Log doesn't appear in the Console at all.I even have The log Tab in the console Set to full, not ScriptOnly.

    What could be the cause of this?
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,232
    upload_2020-2-21_10-41-24.png

    If the first part is on (mine shows 22 messages) and you see 0 after hitting play mode, your Start is not running. Which means either...

    The GameObject is not active
    The script is not enabled
    You've hit an error and it has stopped your code
    the "Start" method is spelled wrong (it's not for you)

    Those are usually the common reasons that Unity's Start isn't running that I can think of.

    Can you show a screenshot of the GameObject the script is on with the component visible in the inspector?
     
    denissuu likes this.
  8. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    112


    Wait a second...As soon as I hit play, the script gets disabled.Why though?



    Edit : Here is the full code if it helps :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3. using System.Collections;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6.  
    7. public class PlayerControls : MonoBehaviour
    8. {
    9.     public Rigidbody2D player;
    10.     private BoxCollider2D boxcoll;
    11.     public float speed = 0.1f;
    12.     public float jump;
    13.     [SerializeField]private LayerMask platformLayerMask;
    14.     private float jumpMultiplier = 300f;
    15.     public const int Health_max = 100;
    16.     public int health;
    17.     public Image HealthBar;
    18.  
    19.  
    20.     private void Start()
    21.     {
    22.         player = GetComponent<Rigidbody2D>();
    23.         boxcoll = GetComponent<BoxCollider2D>();
    24.         health = Health_max;
    25.         Debug.Log("Health is: " + health);
    26.     }
    27.  
    28.     private void Update()
    29.     {
    30.         if(player.position.y <= -85f)
    31.         {
    32.             SceneManager.LoadScene(0);
    33.         }
    34.  
    35.         if (Input.GetMouseButton(0))
    36.         {
    37.             if (Input.mousePosition.x < Screen.width / 5)
    38.             {
    39.                 if (Input.GetMouseButton(0))
    40.                 {
    41.                     player.velocity = new Vector2(-speed, player.velocity.y);
    42.                 }
    43.                 else
    44.                 {
    45.                     player.velocity = new Vector2(0f, player.velocity.y);
    46.                 }
    47.             }
    48.             else if (Input.mousePosition.x > Screen.width / 5 && Input.mousePosition.x < Screen.width / 1.75)
    49.             {
    50.                 if (Input.GetMouseButton(0))
    51.                 {
    52.                     player.velocity = new Vector2(speed, player.velocity.y);
    53.                 }
    54.                 else
    55.                 {
    56.                     player.velocity = new Vector2(0f, player.velocity.y);
    57.                 }
    58.             }
    59.             else if (Input.mousePosition.x > Screen.width / 1.75)
    60.             {
    61.                 if(Input.mousePosition.x > Screen.width / 1.3 && Input.mousePosition.x < Screen.height / 3)
    62.                 {
    63.                     return;
    64.                 }
    65.                 else if(Input.GetMouseButtonDown(0) && isGrounded())
    66.                 {
    67.                     player.velocity = new Vector2(player.velocity.x, jump * jumpMultiplier);
    68.                 }
    69.             }
    70.         }
    71.         else
    72.         {
    73.             //Do nothing for now
    74.         }
    75.  
    76.  
    77.     }
    78.  
    79.  
    80.     private bool isGrounded()
    81.     {
    82.         float extraHeight = 0.2f;
    83.         RaycastHit2D RaycastHit = Physics2D.BoxCast(boxcoll.bounds.center, boxcoll.bounds.size, 0f, Vector2.down, extraHeight, platformLayerMask);
    84.         Color rayColor;
    85.         if(RaycastHit.collider != null)
    86.         {
    87.             rayColor = Color.green;
    88.         }
    89.         else
    90.         {
    91.             rayColor = Color.red;
    92.         }
    93.         Debug.DrawRay(boxcoll.bounds.center + new Vector3(boxcoll.bounds.extents.x, 0), Vector2.down * (boxcoll.bounds.extents.y + extraHeight), rayColor);
    94.         Debug.DrawRay(boxcoll.bounds.center - new Vector3(boxcoll.bounds.extents.x, 0), Vector2.down * (boxcoll.bounds.extents.y + extraHeight), rayColor);
    95.         Debug.DrawRay(boxcoll.bounds.center - new Vector3(boxcoll.bounds.extents.x, boxcoll.bounds.extents.y + extraHeight), Vector2.right * (boxcoll.bounds.extents.x), rayColor);
    96.         return RaycastHit.collider != null;
    97.     }
    98.  
    99.     public int GetHealth()
    100.     {
    101.         return health;
    102.     }
    103.  
    104.  
    105.     public void OnDamageReceived()
    106.     {
    107.         GetHealth();
    108.         health -= 20;
    109.         UpdateHealth();
    110.  
    111.     }
    112.  
    113.     private void Awake()
    114.     {
    115.         HealthBar = transform.Find("HealthBar").Find("GreenHP").GetComponent<Image>();
    116.     }
    117.  
    118.  
    119.     public void UpdateHealth()
    120.     {
    121.         HealthBar.fillAmount = (float)health / Health_max;
    122.     }
    123. }
    124.  
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    8,342
    You likely have another script/component which is disabling this one somewhere. I'd search for everywhere you reference this script in your other scripts. Also verify it is the script being disabled and not the entire GameObject.
     
    denissuu likes this.
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,232
    Doesn't appear there is anything in that script turning itself off or the gameobject off, so as mentioned, make sure there isn't another script doing it.
     
    denissuu and Joe-Censored like this.
  11. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    112
    The thing is that this is a new project, and my only scripts are just a camera follow script, one that restarts the level if you get to the end, and 3 that fades a specific sprite to a color.
    So there's no way that's what makes it be disabled.I checked all of the scripts and they have no linking one to the other or anything.
     
unityunity