0

For some hours i have these problems now:

NullReferenceException: Object reference not set to an instance of an object EnemyScreenSpaceUIScript.Start () (at Assets/Scripts/EnemyScreenSpaceUIScript.cs:34)

NullReferenceException: Object reference not set to an instance of an object EnemyScreenSpaceUIScript.Update () (at Assets/Scripts/EnemyScreenSpaceUIScript.cs:47)

line 34 is in the Start () - Method ( i added a comment)

line 47 is the first line in Update () - Method

this is the EnemyScreenSpaceUIScript i have:

public EnemyScript enemyScript;

public Canvas canvas;
public GameObject healthPrefab;

public float healthPanelOffset = 0.35f;
public GameObject healthPanel;
public Text enemyName;
private Slider healthSlider;

private DepthUIScript depthUIScript;

private Renderer selfRenderer;


// Use this for initialization
void Start () 
{
    GameObject CanvasObj = GameObject.Find("HealthPrefab");
    canvas = CanvasObj.GetComponent<Canvas>();

    enemyScript = GetComponent<EnemyScript>();
    healthPanel = Instantiate(healthPrefab) as GameObject;
    healthPanel.transform.SetParent(canvas.transform, false);

    enemyName = healthPanel.GetComponentInChildren<Text>();
    enemyName.text = enemyScript.monsterName; // <- this is line 34

    healthSlider = healthPanel.GetComponentInChildren<Slider>();

    depthUIScript = healthPanel.GetComponent<DepthUIScript>();
    canvas.GetComponent<ScreenSpaceCanvasScript>().AddToCanvas(healthPanel);

    selfRenderer = GetComponentInChildren<Renderer>();
}

// Update is called once per frame
void Update () 
{
    healthSlider.value = enemyScript.HP / (float)enemyScript.maxHP; //<- this is line 47

    Vector3 worldPos = new Vector3(transform.position.x, transform.position.y + healthPanelOffset, transform.position.z);
    Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos);
    healthPanel.transform.position = new Vector3(screenPos.x, screenPos.y, screenPos.z);

    float distance = (worldPos - Camera.main.transform.position).magnitude;
    depthUIScript.depth = -distance;

    float alpha = 3 - distance / 4.0f;
    depthUIScript.SetAlpha(alpha);

    if (selfRenderer.isVisible && alpha > 0)
    {
        healthPanel.SetActive(true);
    }
    else
    {
        healthPanel.SetActive(false);            
    } 

    if (gameObject.GetComponent<Mob>().health <= 0)
    {
        healthPanel.SetActive(false);
    }
    else
    {

    }
}

i can't figure out what is wrong?

AlpakaJoe
  • 457
  • 4
  • 15
  • Are you sure the EnemyScript is attached to the same GameObject and it is derived from a MonoBehaviour? – Niklas May 12 '18 at 22:09
  • yes, 100% sure. everything looks right for me, i looked over everything now for hours and i can't find why this is doings problems now. The code worked this morning, all i changed was, that i made a new prefab out of the gameObject. – AlpakaJoe May 12 '18 at 22:11
  • Double-click on that error then tell us which line of code is causing it. – Programmer May 12 '18 at 22:13
  • @Programmer i already did in the description: the first one is line 34, the second one is line 47. line 34 is: enemyName.text = enemyScript.monsterName line 47 is: healthSlider.value = enemyScript.HP / (float)enemyScript.maxHP; – AlpakaJoe May 12 '18 at 22:16
  • for `healthSlider.value = enemyScript.HP / (float)enemyScript.maxHP;`, The `healthSlider` or `enemyScript` is null. Put `Debug.Log(healthSlider)` and `Debug.Log(enemyScript)` and see which one says "null" – Programmer May 12 '18 at 22:19
  • Look in the inspector on runtime and check the `enemyScript` field. – Gunnar B. May 12 '18 at 23:01

0 Answers0