0

I have an enemy AI script working that basically follows my player. Right now I have a "public GameObject player" that I have to assign manually by dragging my player prefab onto the slot. But I want to have a lot of enemies in the scene, so I don't want to have to do this manually for each one. How Can I give the EnemyController script a default player to follow?

I have a PlayerManager which I use to pass the position of my player to the enemy with:

public class EnemyController : MonoBehaviour
{
    Transform target;

    // Start is called before the first frame update
    void Start()
    {
        target = PlayerManager.instance.player.transform;
    }

That part works fine. So my thinking was, just make a public variable for the player like this:

public GameObject player = PlayerManager.instance.player;

But that didn't work. I got this error: "NullReferenceException: Object reference not set to an instance of an object"

Thank you so much for any help you can provide!

derHugo
  • 83,094
  • 9
  • 75
  • 115
Jef
  • 11
  • 2
  • Hi and Welcome to StackOverflow! Instead of assigning the value in the declaration, assign the value in Start. From your AI you can have a private Transform target; and in Start() assign target = PlayerManager.instance.player; Your PlayerManager should assign the player variable from Awake to ensure that any calls from Start will refer to a value and not a null. You can also change the type of player to Transform for ease of use. – hijinxbassist Jun 23 '21 at 23:36
  • The most likely reason you get a null reference is that `instance` is null. This can happen when you attempt to access instance before instance has been set (i am guessing in Awake of PlayerManager). Generally i will use a get in the static Instance property, it checks if the instance is null, if it is it searches the scene for the object of type using FindObjectOfType(). if that is null, then there is none in the scene and you will have to act appropriately, either by creating a new GO with that component or throwing an exception. – hijinxbassist Jun 23 '21 at 23:41
  • Please use the correct tags! Note that [`unityscript`](https://stackoverflow.com/tags/unityscript/info) is or better **was** a JavaScript flavor like custom language used in early Unity versions and is long **deprecated** by now! – derHugo Sep 07 '21 at 21:15

1 Answers1

0

With public GameObject player = PlayerManager.instance.player; your field is initialized before the call of EnemyController's constructor, and you provided no control over the value / definition state of PlayerManager.instance.player (PlayerManager or instance or player which can be null and is null in your case). So it's "too early" to use it.

Instead, you can use the event playerJoinedEvent in the PlayerInputManager to assign the enemy to the played which just joined with an event handler, which checks that PlayerManager and instance and player are not null and then assign the player to the target.

Soleil
  • 6,404
  • 5
  • 41
  • 61