- Home /
My variable value doesnt change through a function called by a button?
I have a button in the canvas which calls a function from the PlayerHealth script to set the health to max again, clicking it calls the function correctly (checked using a Debug.Log inside the function) but setting the variable again to maxHealth doesnt change the value at all in it. And if i call a UI slider it shows object reference as null. But it works perfectly on another function at the same class.
It would be really helpful to see some code. I would use Debug.Log statements and ensure that all objects are being properly assigned and in the correct order, make sure scripts are being called properly. Sometimes lines in the Start() function don't align well across scripts.
Player have a component script Health which manages its current health and set the slider in health image in UI called healthbar. button calls the function Health() in shop which calls the sethealthtomax() in health component in player and set health to max. am getting the "hi" in console
For some reason I can't view the images. Try using Debug statements to see if the object is null. Does the slider and button work otherwise? Are you able to receive values from the slider?
healthBar = GameObject.FindGameObjectWithTag("PlayerHealth").GetComponent(); this might return null. and then healthBar.SetHealth(currentHealth); does not work
Answer by sachinchetu · Jun 04, 2020 at 06:46 AM
The error says, you are missing something to assign in Unity. Check your script's public variables properly.