#I want to get health variable from Health script and check if it is changed

1 messages · Page 1 of 1 (latest)

worthy pier
#

namespace BarthaSzabolcs.Tutorial_SpriteFlash.Example
{
    public class SimpleFlashExample : MonoBehaviour
    {
        [SerializeField] Health health;
        [SerializeField] private SimpleFlash flashEffect;
        [SerializeField] private KeyCode flashKey;
        [SerializeField] public int m_health = 12;

        private void Update()
        {
            if(health != m_health)
                if (Input.GetKeyDown(flashKey))
            {
                flashEffect.Flash();
            }
        }
    }
}```
#

But I get this error Assets\SimpleFlashExample.cs(15,16): error CS0019: Operator '!=' cannot be applied to operands of type 'Health' and 'int'

solemn sonnet
#

m_health is an integer

#

health is a Health object

#

what are you trying to do here?

worthy pier
#

If health's value changed I want to make flashEffect.Flash(); happen

solemn sonnet
#

does Health have an integer field that stores your current health?

worthy pier
solemn sonnet
#

okay, then you'll just want to use that field

#

instead of trying to compare the Health component itself to the integer

worthy pier
#

How can I do it I can only put the script there

solemn sonnet
#

if you have a reference to Health, you can access any of its public fields

#
Health health = // get a Health, somehow
health.mana += 5;
health.experience *= 2;
if (health.health != lastHealthValue)
{
  lastHealthValue = health.health;
  DoFlash();
}
worthy pier
#

bro thank you so much for your help 🙏

solemn sonnet
#

np (:

#

for more details