Search Unity

'Object reference is not set to the instance of an object'?

Discussion in 'Physics' started by Deleted User, Oct 16, 2016.

  1. Deleted User

    Deleted User

    Guest

    I'm trying to make a horror game that is similar to Slender. I want the player to pick up "pages", but, when I edit the script, I keep on getting a compiler error saying that the "Object reference is not set to the instance of an object". Here's my code:

    Code (JavaScript):
    1.         if (hit.RaycastHit.collider.gameObject("Page"));

    Can anybody please help me? If so, I'd greatly appreciate it! Thanks! :)
     
  2. romatallinn

    romatallinn

    Joined:
    Dec 26, 2015
    Posts:
    161
    1. How did you define hit variable? If it is like "RaycastHit hit", then you do not have to use it as "hit.RaycastHit.---", but just "hit.----".
    2. Try to check whether hit variable is assigned or not at first. And only then check what the tag of the object is (of course if the variable isn't equal to null).
     
    Deleted User likes this.