Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Access List on Enemy for Random Collectible Drop

Discussion in '2D' started by johnc81, Apr 7, 2021.

  1. johnc81

    johnc81

    Joined:
    Apr 24, 2019
    Posts:
    142
    Hello,

    I have an enemy controller script that has a list holding a couple of different collectible items:
    Code (CSharp):
    1.  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:
    Code (CSharp):
    1. public GameObject deathEffect;
    2.    
    3.     public float chanceToDrop;
    4.  
    5.     private void OnTriggerEnter2D(Collider2D other) {
    6.         if (other.CompareTag("Enemy")) {
    7.             //other.transform.parent.gameObject.SetActive(false);
    8.             Instantiate(deathEffect, other.transform.position, other.transform.rotation);
    9.             PlayerController.instance.Bounce();
    10.  
    11.             EnemyController enemyController = other.gameObject.GetComponent<EnemyController>();
    12.             Debug.Log(enemyController);
    13.             float dropSelect = Random.Range(0, 100);
    14.             if (dropSelect <= chanceToDrop) {
    15.                 Instantiate(enemyController.collectibles[Random.Range(0, enemyController.collectibles.Count-1)], other.transform.position, other.transform.rotation);
    16.             }
    17.         }
    18.     }
    When I run the game, I get the following error in the console:
    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. 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
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,343
    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.
     
    johnc81 likes this.
  3. johnc81

    johnc81

    Joined:
    Apr 24, 2019
    Posts:
    142
    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
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,343
    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.
     
    johnc81 likes this.
  5. johnc81

    johnc81

    Joined:
    Apr 24, 2019
    Posts:
    142
    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 :D

    Thanks,

    J
     
    MelvMay likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,343
    Yeah, we all fall for that one. Amazing what a break does.

    Good luck!