0
\$\begingroup\$

I am making the game in this tutorial: https://www.youtube.com/watch?v=OR0e-1UBEOU

and at 2:53:23 he is coding a LevelController script. I noticed that mine wasn't working the way his was and so I went to try and debug the error. I found that the error was in the function FindObjectsOfType(); You can find my code below.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LevelController : MonoBehaviour
{
    private Enemy[] _enemies;

    private void onEnable()
    {
      _enemies = FindObjectsOfType<Enemy>();
    }

    // Update is called once per frame
    void Update()
    {
      Debug.Log(_enemies);
    }
}

When I run this my debug log returns null values which is confusing because I have 3 objects (Monster, Monster (1) and Monster (2)) which have the Enemy script assigned to them.

What could be going wrong?

Picture

\$\endgroup\$
1
  • \$\begingroup\$ Did those monsters exist at the time this script was first enabled? Or is it possible it was enabled before the monsters were spawned? \$\endgroup\$
    – DMGregory
    Commented May 8, 2020 at 15:40

1 Answer 1

2
\$\begingroup\$

I think it might be a typo.

You have a method named onEnable. This should be OnEnable (Capital O).

You can see all the methods that MonoBehaviour supports here: https://docs.unity3d.com/ScriptReference/MonoBehaviour.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .