#DontDestroyOnLoad UI [SOLVED]

1 messages · Page 1 of 1 (latest)

loud shale
#

this is my hierarchy...

#

i don't know why my UI become like this but before i was trying to instantiate my Player UI it works

#

after my code works like video above i was trying to use an Instantiate object in my GameManager

using System;
using BunionOfDestiny.Game.Data;
using BunionOfDestiny.Menu.Player;
using UnityEngine;

namespace BunionOfDestiny.Game.Main
{
    public class GameManager : MonoBehaviour
    {
        [SerializeField] private CharacterData characterData;
        [SerializeField] private ObstacleData obstacleData;
        [SerializeField] private PlayerUI playerUI;
        [SerializeField] private Transform[] menu;


        private void Awake()
        {
            characterData.Health += OnHealthChange;
            obstacleData.Obstacle += OnTakeDamage;
        }

        private void Start()
        {
            var prefab = Instantiate(playerUI, menu[0]);
        }

        private void OnDestroy()
        {
            characterData.Health -= OnHealthChange;
            obstacleData.Obstacle -= OnTakeDamage;
        }

        private void OnTakeDamage(int value)
        {
            int hp = characterData.health - value;
            characterData.CharacterHealth(hp);
        }

        private void OnHealthChange(int value)
        {
            playerUI.SetHealth(value);
        }
    }
}
#

After adding Instantiate my UI won't work. the first video in here

rotund sedge
#

you are setting the health on the prefab object, not on the instance

loud shale
rotund sedge
#
        private void Start()
        {
            var prefab = Instantiate(playerUI, menu[0]);
        }

you use the object in playerUI as the prefab for instantiation ( the object ending up in var prefabis the actual instance, not the prefab)
you then call playerUI.SetHealth(value); where playerUI is still the prefab and not the actual object in your scene, which results in the object in your scene not changing

loud shale
loud shale
#

but why is that when i delete this one

        private void Start()
        {
            var prefab = Instantiate(playerUI, menu[0]);
        }

the UI stay the same? not changing at all

rotund sedge
#

presumably because you use a scene object as the prefab, so after instantiation you have 2 objects in the same place

loud shale
#

this is on the play mode there's no other PlayerUI except on Don'tDestroyOnLoad.

rotund sedge
#

whats menu[0]? you instantiate as the child of that object.

loud shale
#

it's just a Transform that i didn't have use for that anymore.

loud shale
#

with different UI. (so the playerUI just for testing).