- Home /
When changing UI Image renders over the last one
When changing the sprite it renders over the last one. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI;
public class UpdateBuyMenuStats : MonoBehaviour, IPointerEnterHandler
{
public string weaponName;
Weapon weapon;
Sprite sprite;
void Start()
{
weaponName = gameObject.GetComponent<BuyWeapon>().weaponName; WeaponList wl = new WeaponList();
foreach (Weapon wp in wl.weaponList)
{
if (wp.name == weaponName)
{
weapon = wp;
}
}
sprite = Resources.Load<Sprite>(weapon.spriteName);
}
public void OnPointerEnter(PointerEventData eventData)
{
GameObject.Find("FirerateText").GetComponent<Text>().text = "Firerate: " + weapon.firerate.ToString();
GameObject.Find("InitialAmmoText").GetComponent<Text>().text = "Ammo: " + weapon.initialAmmoCount.ToString();
GameObject.Find("DamageText").GetComponent<Text>().text = "Damage: " + weapon.damage.ToString();
GameObject.Find("BulletSpeedText").GetComponent<Text>().text = "Bullet speed: " + weapon.speed.ToString();
GameObject.Find("Thumbnail").GetComponent<Image>().sprite = sprite;
}
}
Hi @1234filip - "When changing the sprite it renders over the last one." doesn't tell much. The question is hard to understand.
If you change image component sprite, was it already over "the last one" whatever you refer to with that.
UI system depends mostly on hierarchy of UI objects, how items are laid out. There is top to bottom order of rendering. Bottom items in hierarchy window list will render over other items on Canvas. But you didn't post you hierarchy image.
No I'm just changing the sprite but when I change it, it shows up but the previous sprite is still there. The hierarchy doesn't apply here as I'm just changing the sprite property of a component. The gif of the problem is at the bottom of the post.
Your answer
