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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help Please! Getting error: "Object reference not set to an instance of an object" when I shoot!

Discussion in 'Scripting' started by harrypotterindy, May 14, 2015.

  1. harrypotterindy

    harrypotterindy

    Joined:
    Dec 27, 2014
    Posts:
    15
    Hi,
    I am getting an error in my code saying:

    NullReferenceException: Object reference not set to an instance of an object
    MachinegunShooting.Update () (at Assets/Scripts/MachinegunShooting.cs:31)


    I am really quite confused as to what it is saying for I use this same sort of line of code in TONS of different projects and they work but apparently this one doesn't. Here is my code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MachinegunShooting : MonoBehaviour {
    6.  
    7.    public float CoolDownTime = 0.2f;
    8.    public Animation shootAnim;
    9.    public GameObject Machinegun;
    10.  
    11.    //Cooldown
    12.    private float CoolDown;
    13.  
    14.    // Use this for initialization
    15.    void Start()
    16.    {
    17.      Machinegun.SetActive(true);
    18.    }
    19.  
    20.    // Update is called once per frame
    21.    void Update()
    22.    {
    23.      //CoolDown<Time.time means that current game time is greater than our cooldown time which means we can fire again
    24.      if (Input.GetKey(KeyCode.Mouse0) && CoolDown<Time.time)
    25.      {
    26.        Ray ray = new Ray(transform.position, transform.forward);
    27.        RaycastHit hit;
    28.  
    29.        GetComponent<AudioSource>().Play();
    30.        GetComponent<Animation>().Play();
    31.  
    32.        if(hit.collider.CompareTag("Enemy")) {
    33.          Destroy(hit.collider.transform);
    34.        }
    35.  
    36.        //CoolDown = current in game time + cooldown time
    37.        CoolDown = Time.time + CoolDownTime;
    38.  
    39.        if (Physics.Raycast(ray, out hit))
    40.        {
    41.  
    42.        }
    43.      }
    44.    }
    45. }
    46.  
    Help will be much appreciated!
     
  2. HAlbera

    HAlbera

    Joined:
    Jun 7, 2013
    Posts:
    63
    Seems to me you are checking the raycast before casting it. Then you are trying to access the tag of the object you havent hit, nor even cast a ray to hit?

    Put the if (physics.ray... bit around the if (hit.collider... bit. And you should be, somewhat, golden.

    What you need to be careful of is what order you are doing things. If you try to do something to an object, make sure you KNOW that object is going to be availiable at that time. Your code tries to do something to the tag of the object regardless of whether it is there or not.

    Im on my phone or i would be more verbose for ya.

    EDIT: Also for the cooldown i would suggest simply having a function called ResetCooldown() and have that set the machinegun state to active. Then inside your Input check immediately set that state to off and Invoke("ResetCooldown", 1f); replace the second parameter with whatever time you want (as a float) or make a public float variable that you can set in the inspector and chuck it in there.

    Hope that helps!
     
    Last edited: May 14, 2015
  3. harrypotterindy

    harrypotterindy

    Joined:
    Dec 27, 2014
    Posts:
    15
    Thank you HAIbera! I am very new at coding so I will be on these forums quite a bit... especially for the fact that I'm trying to develop a Zombie FPS all on my own... and I am only 10 years old... and I've only been learning to make games since I was like, eight. If you would like to help me making this game you can always E-Mail me at:
    harper@harpernicholson.ca
    Again, thank you!