Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

288
Vistas
Unity null exception when Instantiating class

I somehow get a null reference exception in Unity. I made a constructor for my weapon class and made another weapon class (which inherits from monobehaviour), where it creates the weapons with its stats. in pistol.Stats(10, 0.5f, 2, 5, new Vector2(0.2f, 0.09f)); happens a null reference.

Script 1:

public class WeaponClass
{
    public float damage, spread, bulletsPerSec, rotationSpeed;
    public string name;
    public Vector2 shootPoint;
    private Image image;

    public WeaponClass Stats(float Adamage, float Aspread, float AbulletsPerSec, float ArotationSpeed, Vector2 Ashootpoint)
    {
        damage = Adamage;
        spread = Aspread;
        bulletsPerSec = AbulletsPerSec;
        rotationSpeed = ArotationSpeed;
        shootPoint = Ashootpoint;
        return this;
    }
}

Script 2:

public class Weapons : MonoBehaviour
{
    private PlayerControlls _playerControlls;
    private WeaponClass pistol, m4 = new WeaponClass();
    private Bullet _bullet;
    public WeaponClass currentWeapon;
    private GameObject bullet;
    public Rigidbody2D bulletRb;
    private float randomdirection;
    public float randomdirectionangle;
    
    void Start()
    {
        pistol.Stats(10, 0.5f, 2, 5, new Vector2(0.2f, 0.09f));
        currentWeapon = pistol;
        Debug.Log(currentWeapon);
    }
    
    void Update()
    {
        
    }

    public void Shoot()
    {
        randomdirection = Random.Range(-currentWeapon.spread, currentWeapon.spread);
        randomdirectionangle = Mathf.Atan2(randomdirection, currentWeapon.shootPoint.x) * Mathf.Rad2Deg;
        Quaternion bulletrotation = new Quaternion(0, 0, randomdirectionangle, 0);
        
        GameObject bulletclone = Instantiate(bullet, currentWeapon.shootPoint, _playerControlls.GunEquipped.transform.rotation);
        bulletRb = bulletclone.GetComponent<Rigidbody2D>();
    }
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Careful when declaring/initializing multiple fields in a single line!

What you do in

private WeaponClass pistol, m4 = new WeaponClass();

equals writing

private WeaponClass pistol; // NOT INITIALIZED!
private WeaponClass m4 = new WeaponClass();

So what you want would be

private WeaponClass pistol = new WeaponClass(), m4 = new WeaponClass();

I would in general discourage from using this at all and rather always do it in separate lines:

private WeaponClass pistol = new WeaponClass();
private WeaponClass m4 = new WeaponClass();

as this is

  • better readable
  • as you see less error prone
  • better maintainable, you can easily add or remove fields this way
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda