Access List on Enemy for Random Collectible Drop

Hello,

I have an enemy controller script that has a list holding a couple of different collectible items:

 public List<Transform> collectibles = new List<Transform>();

I add these collectibles in the Inspector.

I have another script attached to my player which uses OnTriggerEnter2D to detect the collision with an enemy and drops one of the items. I am struggling to get it to access the list from the enemy controller script to instantiate a random collectible:

public GameObject deathEffect;
   
    public float chanceToDrop;

    private void OnTriggerEnter2D(Collider2D other) {
        if (other.CompareTag("Enemy")) {
            //other.transform.parent.gameObject.SetActive(false);
            Instantiate(deathEffect, other.transform.position, other.transform.rotation);
            PlayerController.instance.Bounce();

            EnemyController enemyController = other.gameObject.GetComponent<EnemyController>();
            Debug.Log(enemyController);
            float dropSelect = Random.Range(0, 100);
            if (dropSelect <= chanceToDrop) {
                Instantiate(enemyController.collectibles[Random.Range(0, enemyController.collectibles.Count-1)], other.transform.position, other.transform.rotation);
            }
        }
    }

When I run the game, I get the following error in the console:

NullReferenceException: Object reference not set to an instance of an object
StompBox.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/StompBox.cs:21)

Line 21 refers to the line where I instantiate the random collectible. My Debug returns NULL, so I am obviously doing something wrong when trying to access the List, but I don’t know what?

Many Thanks,

J

if I’m following what you’re saying then “other.gameObject” does have a tag “Enemy” but it doesn’t have an “EnemyController” script. Just debug which gameObject this is and check it.

1 Like

Hello,

The enemy has a tag called Enemy and a script called EnemyController. The problem I have is that I don’t know how to access the List I created on the EnemyController from the StompBox script attached to the Player.

The idea is that when the Player jumps on the Enemy, the StompBox script on the Player accesses the List on the EnemyController to select a random collectible to Instantiate.

Many Thanks,

J

You do know because you’re doing it correctly.

This seems to be indicating “Debug.Log(enemyController);” is returning NULL. This means you’re not getting this script and my previous comment deals with that.
So is “enemyController” NULL or not? If it is then again, my previous comment stands. You’re assuming the “other” is indeed an enemy and that it has such a script. You might want it to but Unity is saying that object doesn’t have such a script.

If it’s something else in that “instantiate” that’s NULL then please specify.

1 Like

Hi,

Thank you. I took a little walk, cleared my head, came back and realised the script was attached to a parent object. All sorted. Thought I was going crazy…need to take breaks more often :smile:

Thanks,

J

1 Like

Yeah, we all fall for that one. Amazing what a break does.

Good luck!