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.

Question How can I convert an array of components into their gameobjects?

Discussion in 'Scripting' started by HansSchmulge, Sep 9, 2023.

  1. HansSchmulge

    HansSchmulge

    Joined:
    Nov 29, 2022
    Posts:
    7
    Hello.
    I'm trying to make a weapon pickup object.
    As planned, when my car hits an object, the “Weapon Change” script is launched.
    It checks for available gunPlace of the appropriate type and creates a weapon there.

    And everything would be fine, but I constantly catch NullReferenceException when trying to translate a component into a game object.


    Please tell me what's wrong. I will be grateful.

    Here is my script so far:
    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class GunChange : MonoBehaviour
    5. {
    6.     private List<GameObject> gunplaces;
    7.     [SerializeField] GameObject turretBase; // keeps weapon prefab
    8.     private GunType.Type type;
    9. void Update()
    10.     {
    11.         transform.Rotate(0, 25f * Time.deltaTime, 0); // rotates object to pick up
    12.     }
    13.  
    14.     void OnTriggerEnter(Collider other)
    15.     {
    16.         if(other.CompareTag("Player"))
    17.         {
    18.             FindGunPlace(other.gameObject);
    19.             Destroy(gameObject);
    20.         }
    21.     }
    22.     void FindGunPlace(GameObject player)
    23.     {
    24.         var Type = turretBase.GetComponentInChildren<GunBaseScript>();
    25.         type = Type.weaponCharacteristics.gunType; //Get gunType in gun prefab: small, main or support.
    26.  
    27.         var gunplacesProt = player.GetComponentsInChildren<GunPlaceScript>(); //Get all the gun places of your car.
    28.         for (int i = 0; i < gunplacesProt.Length; i++)
    29.         {
    30.             if (gunplacesProt[i].gunType == type) //if the place is of the appropriate type, then add it to the list of appropriate gun places.
    31.             {
    32.                 gunplaces.Add(gunplacesProt[i].gameObject); //and there is a NullReferenceException
    33.             }
    34.         }
    35.     }
    36. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    37,265
    The answer is always the same... ALWAYS!

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
    bugfinders likes this.
  3. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    549
    It appears that GetComponentInChildren returns an array of Component objects in which case you need to cast the item to a GunPlaceScript.

    Inside your for loop (if you don't want to use foreach syntax

    Code (CSharp):
    1. var gunPlace = (GunPlaceScript)gunplacesProt[i];
    and reference gunPlace from then on
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,757
    You absolutely do NOT need to cast the results of GetComponentsInChildren, and this has nothing to do with the problem at hand.

    The OP's issue is very simple. They never initialized
    gunPlaces
    . It needs to be initialized:

    private List<GameObject> gunPlaces = new();
     
    tleylan likes this.