[SOLVED] Weird null exception error

My game has a weird null exception error that I keep on getting everytime I start the game. It only appears once in the console window.

NullReferenceException: Object reference not set to an instance of an object
StatusIndicator.Awake () (at Assets/Scripts/StatusIndicator.cs:15)

StatusIndicator.cs:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class StatusIndicator : MonoBehaviour
{
  [SerializeField] private RectTransform healthBarRect;

  private Quaternion rotation;
  private Vector3 position;

  void Awake()
  {
    rotation = transform.rotation;
    position = transform.parent.position - transform.position;
  }

  void Update()
  {
    transform.rotation = rotation;
    transform.position = transform.parent.position - position;
  }

  public void SetHealth(float _cur, float _max)
  {
    float value = _cur / _max;

    healthBarRect.localScale = new Vector3(value, healthBarRect.localScale.y, healthBarRect.localScale.z);
  }
}

Not really sure why this is happening. My game works perfectly fine even with the error. If you’re not sure what I’m doing between line 12-22, I’m restricting my StatusIndicator (health bar) to rotate with the parent (enemy) and also make sure position is aligned correctly. If anyone have any idea why it’s happening, let me know.

Hi

The fact that it happens only once is understandable: Awake is called only once.

The only explanation for this exception is if transform.parent is null (but this exception would leave position uninitialized, unless you set its value somewhere else in the code).

You can check that with something like:

Debug.Log(System.Object.ReferenceEquals(transform.parent, null));

hope this helps

Adriano

it’s most likely checking for the parent transform be for the parent transform has been started. Test this by changing awake() to start()

When I add the debug statement, it tells me this in the console window:

False
UnityEngine.Debug:Log(Object)
StatusIndicator:Awake() (at Assets/Scripts/StatusIndicator.cs:16)
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
<SpawnLoop>c__Iterator0:MoveNext() (at Assets/Scripts/SpawnController.cs:58)

Not quite sure what that meant but I’m guessing it has something to do with my SpawnController script?

Small version of SpawnController.cs (line 21 is supposed to be line 58 in the original version:

IEnumerator SpawnLoop()
{
  foreach (Waves wave in waves)
  {
    m_CurrentWave = wave;
    foreach (WaveSystem system in wave.systems)
    {
      if (system.delay > 0)
      yield return new WaitForSeconds(system.delay);

      if (system.enemies != null && system.enemyCount > 0)
      {
        for (int i = 0; i < system.enemyCount; i++)
        {
          if (system.isFlock)
          {
            Instantiate(system.enemies, spawn.position + new Vector3(Random.Range(-2, 2), Random.Range(-2, 2), 0f), Quaternion.identity);
          }
          else
          {
            Instantiate(system.enemies, new Vector2(Random.Range(-15f, 15f), Random.Range(-15f, 15f)), Quaternion.identity); // Line 58
          }
        }
      }
    }
    yield return null;
  }
  yield return null;
}

Nah, the error now shows every update instead of once only.

The exception is thrown if your StatusIndicator doesn’t have a parent transform. None of the code you have shown is setting the parent, so this is happening due to something else.

Ohh… I have one status indicator for my player in my game that doesn’t have a parent. The error resolves when I attach it to my player. Thanks.