Search Unity

Errors that I do not know solve

Discussion in 'Scripting' started by Nixel2013, May 11, 2019.

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    This is the problem:

    NullReferenceException: Object reference not set to an instance of an object
    PlayerShooting.Awake () (at Assets/Script/PlayerShooting.cs:41)
    UnityEngine.Object:Instantiate(GameObject, Vector3, Quaternion)
    PuntodeDisparo:Update() (at Assets/Script/PuntodeDisparo.cs:31)


    and, this is the lines:
    -----------------------------------------------------------------------------------------------------------------------------
    PlayerShooting
    private void Awake()
    {

    shootableMask = LayerMask.GetMask("Shootable");
    gunParticle = GetComponent<ParticleSystem>();
    //gunLine = GetComponent<LineRenderer>();
    //gunAudio = GetComponent<AudioSource>();
    gunLigth = GetComponent<Light>();
    currentBullet = MaxBullet;
    StartCoroutine(ReloadAmmo());
    enemyImage.enabled = true;
    enemyHealthSlider.gameObject.SetActive(false);
    }
    -------------------------------------------------------------------------------------------------------------------
    PuntodeDisparo
    void Update()
    {
    {
    if (Input.GetButton("Fire1") && Time.time > nextFire)
    {
    nextFire = Time.time + fireRate;



    Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    }

    }

    }
     
  2. Please, put your code in code tags. It's unreadable. And please put the whole scripts here, because for example you have an error regarding
    Code (CSharp):
    1. enemyImage
    , but there is no other info about it in your code above.
     
    Nixel2013 likes this.
  3. xenonmiii

    xenonmiii

    Joined:
    Aug 2, 2010
    Posts:
    147
    @Nixel2013 Hope you solved your issue. But if you haven't, to solve NREs (Null Reference Exceptions) you just need to check which line it's bombing out on, and make sure that a reference is set.

    I don't have the exact line numbers so I'm going to guess the lines that are causing the NRE.

    The stack traces complains that something in Awake is causing it (PlayerShooting.Awake () (at Assets/Script/PlayerShooting.cs:41))

    I think the two possible offenders are
    • enemyImage.enabled = true;
    • enemyHealthSlider.gameObject.SetActive(false);
    I'm assuming these are public variables that you assign in the Editor. Can you check if you have actually set a reference in the Unity editor for enemyImage and enemyHealthSlider?
     
    Nixel2013 likes this.