- Home /
Public GameObject not assignable in the inspector.
I'm creating a basic health and damage script, and I was getting to the part where you declare the GameObject as dead. I have created a public GameObject variable, but it doesn't seem to appear in the inspector, therefore not allowing me to assign a GameObject. I have checked other posts on this issue, but none of them resolved my issue. I dont have any addons or assets that would be messing with the way the inspector works, and from what I see there is nothing that should be conflicting with it.
using UnityEngine;
using System.Collections;
public class GunTarget : MonoBehaviour
{
public float health = 100f;
public GameObject EntityObject;
public void TakeDamage (float amount)
{
health -= amount;
if (health <= 0f)
{
Death();
}
}
public void Death()
{
Destroy(EntityObject);
}
}
my best guess is that it's something to do with the "death" method, and not the GameObject itself. Any help would be appreciated, let me know if you need more information. Thank you in advance.
Do you have ANY error in the console? If so, fix them, even if they are not related to this script
Is the name of the file EXACTLY matching the name class? (case included)
Answer by ValerijD · Nov 15, 2021 at 11:32 PM
Make the field private and add the property [SerializeField] directly above your field:
[SerializeField]
private GameObject entityObject;