【Unity】获取Slider组件报错
项目场景:
通过传入数据调整Slider组件的value值,实现血量随数据更新
问题描述
通过GetComponent<>()获取Slider报错
ArgumentException: GetComponent requires that the requested component ‘Slider’ derives from MonoBehaviour or Component or is an interface.
using Tools;
using UI.Base;
using UnityEngine;
using UnityEngine.UIElements;
namespace UI {
public class MainUI : MonoBehaviour{
[Header("子界面")]
[SerializeField]private GameObject HPSlider;
private Slider slider;
private new void Start() {
slider = HPSlider.GetComponent<Slider>();
}
private void UpdateHp(Message message) {
slider.value = (float)message.Body;
}
}
}
原因分析:
使用错误的引用using UnityEngine.UIElements;

解决方案:
使用正确的引用using UnityEngine.UI;

本文来自博客园,作者:星空探险家,转载请注明原文链接:https://www.cnblogs.com/PuppetLazy/p/17840904.html

浙公网安备 33010602011771号