#DontDestroyOnLoad UI [SOLVED]
1 messages · Page 1 of 1 (latest)
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
At first i was just drag and drop my Player UI into my hierarchy that had a DontDestroy Script. https://www.toptal.com/developers/hastebin/qibovewufu.cpp
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
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
you are setting the health on the prefab object, not on the instance
im sorry but what do you mean by that? setting the health on the prefab object?
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
ooh yes, that was when im trying to add Instantiate and then after remove it. its still same
This video was before im adding instantiate.
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
presumably because you use a scene object as the prefab, so after instantiation you have 2 objects in the same place
this is on the play mode there's no other PlayerUI except on Don'tDestroyOnLoad.
whats menu[0]? you instantiate as the child of that object.
it's just a Transform that i didn't have use for that anymore.
All of this code were being erased for now. and gonna use that for later.
with different UI. (so the playerUI just for testing).