Public fields not showing in inspector
Public variables at line 8-9 are not showing in inspector. I was following the Unity 'Tanks!' tutorial and it said to drag/drop the slider and image into the fields, but I cannot find a way to show them in the inspector.Here is my code:
using UnityEngine;
using UnityEngine.UIElements;
public class TankHealth : MonoBehaviour
{
public float m_StartingHealth = 100f;
public Slider m_Slider;
public Image m_FillImage;
public Color m_FullHealthColor = Color.green;
public Color m_ZeroHealthColor = Color.red;
public GameObject m_ExplosionPrefab;
private AudioSource m_ExplosionAudio;
private ParticleSystem m_ExplosionParticles;
private float m_CurrentHealth;
private bool m_Dead;
private void Awake()
{
m_ExplosionParticles = Instantiate(m_ExplosionPrefab).GetComponent<ParticleSystem>();
m_ExplosionAudio = m_ExplosionParticles.GetComponent<AudioSource>();
m_ExplosionParticles.gameObject.SetActive(false);
}
private void OnEnable()
{
m_CurrentHealth = m_StartingHealth;
m_Dead = false;
SetHealthUI();
}
public void TakeDamage(float amount)
{
// Adjust the tank's current health, update the UI based on the new health and check whether or not the tank is dead.
m_CurrentHealth -= amount;
SetHealthUI();
if (m_CurrentHealth <= 0f && !m_Dead)
{
OnDeath();
}
}
private void SetHealthUI()
{
// Adjust the value and colour of the slider.
m_Slider.value = m_CurrentHealth;
m_FillImage.tintColor = Color.Lerp(m_ZeroHealthColor, m_FullHealthColor, m_CurrentHealth / m_StartingHealth);
}
private void OnDeath()
{
// Play the effects for the death of the tank and deactivate it.
m_Dead = true;
m_ExplosionParticles.transform.position = transform.position;
m_ExplosionParticles.gameObject.SetActive(true);
m_ExplosionParticles.Play();
m_ExplosionAudio.Play();
gameObject.SetActive(false);
}
}
I am using Unity version 2019.2 and they are using an older version. i am only using 2019.2 because the tutorial download said it required that version. I am on a windows 10 computer and the editor is Visual Studio 2019. here is the video if you need it: https://youtu.be/XRzGfRkZrNM?t=2400
Replace using UnityEngine.UIElements;
by using UnityEngine.UI;
Answer by fumetsuhito95 · Sep 26, 2020 at 11:38 AM
Try namespace UnityEngine.UI not UnityEngine.UIElements.
I was facing the same problem and found this solution in StackOverflow.