Having a strange occurrence when i build the game

my health and an enemy’s health (idk who) decrease until stopping at 0 when i start the game and the enemy Ai stops working however i can still attack and run around everything is normal besides those?

is runs fine in unity Btw just not when its built

Hmmm… not a lot to go on with that…
So is an enemy just dropping in health until it dies? Is your UI dropping steadily, or is it suddenly just 0?

so after i build it the health for my player and the health for and enemy decrease gradually to zero but no one dies im not near anything to to fight anything but when i go to an enemy after strating they just stand there and i can attack untill they die

That is very odd behavior, especially if it’s only happening in built game.
Paste in your Health.cs script, and we’ll see if we can find the cause.

using UnityEngine;
using RPG.Saving;
using RPG.Stats;
using RPG.Core;
using RPG.Utils;
using System;

namespace RPG.Attributes
{
    public class Health : MonoBehaviour, ISaveable
    {
        [SerializeField] float regenrationPercentage = 70f;

        BaseStats baseStats;

        LazyValue<float> healthPoints;

        bool isDead = false;

        private void Awake()
        {
            healthPoints = new LazyValue<float>(GetInitialHealth);
        }

        private float GetInitialHealth()
        {
            return baseStats.GetStat(Stat.Health);
        }

        private void Start()
        {
            baseStats = GetComponent<BaseStats>();
            healthPoints.ForceInit();

            baseStats.onLevelUp += RegenerateHealth;
            if (healthPoints.value < 0)
            {
                healthPoints.value = baseStats.GetStat(Stat.Health);
            }
        }

       /* private void OnEnable()
        {
            baseStats.onLevelUp += RegenerateHealth;
        }

        private void OnDisable()
        {
            baseStats.onLevelUp -= RegenerateHealth;
        }*/


        public bool IsDead()
        {
            return isDead;
        }

        public void TakeDamage(GameObject instigator, float damage)
        {
            print(gameObject.name + " took damage: " + damage);

            healthPoints.value = Mathf.Max(healthPoints.value - damage, 0);

            if(healthPoints.value == 0)
            {
                Die();
                AwardExperience(instigator);
            }
        }

        public float GetHealthPoints()
        {
            return healthPoints.value;
        }

        public float GetMaxHealthPoints()
        {
            return baseStats.GetStat(Stat.Health);
        }


        public float GetPercentage()
        {
            return 100 * (healthPoints.value / baseStats.GetStat(Stat.Health));
        }

        private void Die()
        {
            if (isDead) return;

            isDead = true;
            GetComponent<Animator>().SetTrigger("die");
            GetComponent<ActionScheduler>().CancelCurrentAction();
        }
        private void RegenerateHealth()
        {
            float regenHealthPoints = baseStats.GetStat(Stat.Health) * regenrationPercentage / 100;
            healthPoints.value = Mathf.Max(healthPoints.value, regenHealthPoints);
        }

        private void AwardExperience(GameObject instigator)
        {
            Experience experience = instigator.GetComponent<Experience>();

            if (experience == null) return;

            experience.GainExperience(baseStats.GetStat(Stat.ExperienceReward));
        }

        public object CaptureState()
        {
            return healthPoints.value;
        }
        public void RestoreState(object state)
        {
            healthPoints.value = (float)state;

            if(healthPoints.value == 0)
            {
                Die();
            }
        }
    }
}

the reason on enable and on disable are commented out is because i got a null reference and was like it worked without it

The OnEnable/OnDisable can be fixed by moving the line
baseStats = GetComponent<BaseStats>();
to Awake(). Put this BEFORE the healthPoints assignment. By assigning it in Start(), you have a risk that something else will try to access GetHealthPoints() or GetMaxHealthPoints() and you’ll risk null reference errors.

I’m not sure that’s the cause of your mysterious health shrinking, however. The only place where we change the healthPoints.value is in the TakeDamage, and that should be spamming the console with “xxx took Damage” messages.

See if that change helps, then paste in your UI code for the health bars if that didn’t do the trick.

that fixed the null ref with on enable im so used to calling get component in start i didnt even think that was causing that do you want the enemyhealth display and the health display?

Yes, both of them.
Sam and Rick frequenly put reference caches in Start(), but if the component exists on the same GameObject, it is always better to cache them in Awake().

using System.Collections;
using System;
using TMPro;
using UnityEngine.UI;
using UnityEngine;

namespace RPG.Attributes
{
    public class HealthDisplay : MonoBehaviour
    {
        Health health;

        private void Awake()
        {
            health = GameObject.FindWithTag("Player").GetComponent<Health>();
        }

        private void Update()
        {
            GetComponent<TMP_Text>().text = String.Format("{0:0}/{1:0}", health.GetHealthPoints(), health.GetMaxHealthPoints());
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using TMPro;
using System;
using UnityEngine;
using RPG.Attributes;

namespace RPG.Combat
{
    public class EnemyHealthDisplay : MonoBehaviour
    {
        Fighter fighter;

        private void Awake()
        {
            fighter = GameObject.FindWithTag("Player").GetComponent<Fighter>();
        }

        private void Update()
        {
            if (fighter.GetTarget() == null)
            {
                GetComponent<TMP_Text>().text = "N/A";
                return;
            }
            Health health = fighter.GetTarget();
            GetComponent<TMP_Text>().text = String.Format("{0:0}/{1:0}", health.GetHealthPoints(), health.GetMaxHealthPoints());
        }
    }
}

i just did develompent build and noticed i get some null references?

A scripted object (probably O3DWB.CoordinateSystemRenderSettings?) has a different serialization layout when loading. (Read 32 bytes but expected 140 bytes)

Did you #ifdef UNITY_EDITOR a section of your serialized properties in any of your scripts?

The referenced script (O3DWB.PrefabActivationSettings) on this Behaviour is missing!

The referenced script on this Behaviour (Game Object ‘’) is missing!

if this helps this was repeating in the player logs so unity just having issues serializing?

so after removing octave 3d world build from the scene (thats what those errors were for) im getting different ones not related to octave if you want i can screen shot some of the log?

I FIXED IT, i think: i apologize for using up time but when i was doing prefab variants and making enemys i had them tagged as player i just did a dev build and its all working normally so i guess the game got confused at somepoint only on build

That could cause the issue… The Player is like The Highlander. There can be only one.

i feel like 90% of the issues i run into is because i miss something in the inspector :sweat_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms