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:
using System.Collections.Generic;
using UnityEngine;
public class GunChange : MonoBehaviour
{
private List<GameObject> gunplaces;
[SerializeField] GameObject turretBase; // keeps weapon prefab
private GunType.Type type;
void Update()
{
transform.Rotate(0, 25f * Time.deltaTime, 0); // rotates object to pick up
}
void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player"))
{
FindGunPlace(other.gameObject);
Destroy(gameObject);
}
}
void FindGunPlace(GameObject player)
{
var Type = turretBase.GetComponentInChildren<GunBaseScript>();
type = Type.weaponCharacteristics.gunType; //Get gunType in gun prefab: small, main or support.
var gunplacesProt = player.GetComponentsInChildren<GunPlaceScript>(); //Get all the gun places of your car.
for (int i = 0; i < gunplacesProt.Length; i++)
{
if (gunplacesProt[i].gunType == type) //if the place is of the appropriate type, then add it to the list of appropriate gun places.
{
gunplaces.Add(gunplacesProt[i].gameObject); //and there is a NullReferenceException
}
}
}
}