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. Dismiss Notice

NullReferenceExpection

Discussion in 'Scripting' started by s.brown9615, Apr 25, 2014.

  1. s.brown9615

    s.brown9615

    Joined:
    Apr 23, 2014
    Posts:
    2
    I am a high school student and my high school offers a course in unity but its self directed. So there is little help...
    Im getting an error
    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Gun.CanShoot () (at Assets/Scripts/Gun.cs:107)
    3. Gun.Shoot () (at Assets/Scripts/Gun.cs:49)
    4. Gun.ShootContinuous () (at Assets/Scripts/Gun.cs:83)
    5. PlayerController.Update () (at Assets/Scripts/PlayerController.cs:51)
    and the 2 scripts you need
    http://pastebin.com/bVjPVEqF
    http://pastebin.com/ezgwPh77

    Please this would help alot!
     
  2. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    Part of self-direction is asking for help, so you've got it, and I'm sure someone is sifting through the code right now with their spare time, probably waiting for answers themselves... meanwhile though I can give you some helps for other ways to learn on your own.

    First thing is the error, this is a common error, it's a "Null Reference Error", meaning something being referred to is null, it has no entry, and a line is missing to activate or fill it.

    Next is the debug log, it's breaking it down to four lines this error is running through, all of which are connected. I'm guessing something like Update has Shoot, which contains in some order, CanShoot and ShootContinuous? Look at each of the lines in the debug log and figure out what the top level is being caught with an error. Probably CanShoot or ShootContinuous on lines 107 and 83 respectively, there is a variable that is empty, it has nothing in it when the code gets there.

    This could be for a couple reasons:
    1. You haven't "initiated" a variable you're now filling. For instance, if I make a private Vector3, I can't use it until I assign a "new" Vector3, or an already existing Vector3. So one of these lines could be missing a "new" keyword, or one of the variables might need to be initiated earlier in the code, so the assignment has somewhere to go.

    2. You've assigned something that is also null, or doesn't exist. So double check that the variable you're filling it with is also a valid variable at that point.

    I hope this helps, if not someone else will surely come along eventually. Helping each other is how we make things move forward faster, the world would crawl if it didn't happen constantly, good luck!
     
  3. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Change this line
    Code (csharp):
    1. RaycastHit hit;
    To
    Code (csharp):
    1. RaycastHit hit=new RaycastHit();
    You need to set the raycasthit to an actual object, but you're just declaring the variable.
    Null references are one of the easiest things to fix. The error tells you exactly where the problem is.
     
  4. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    You haven't initialized the playercontroll variable anywhere in your code, so it's going to be null.

    If you don't know why you need to initialize reference variables or how to do it, then you're probably need to spend some time reading up on C# and programming.
     
    Last edited: Apr 25, 2014
  5. Tiki

    Tiki

    Joined:
    Mar 3, 2013
    Posts:
    299
    "new" Keyword :p

    Debugging will teach you a lot about your syntax, just keep at it. Google the error as well when you haven't encountered it before, most errors you'll receive early on are common and can be explained either through explanation or demonstration, fixing them will help you understand the importance of, impact of, and proper use of important concepts like declaring, initiating, modifying and converting variables.
     
  6. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    That's right in the general case but is incorrect in this particular scenario since PlayerController is a MonoBehaviour.
     
    Last edited: Apr 25, 2014
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    You should go read how Physics.Raycast works :)
     
  8. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    542
    Ouch. Or I should just not try to wake up and start right on answering questions on my phone.
     
  9. s.brown9615

    s.brown9615

    Joined:
    Apr 23, 2014
    Posts:
    2
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    playerControll is null which means you've never given it a value. That's the bug.