i'm getting this error when the collider colides with something:

Object reference not set to an instance of an object

It's referencing to the gamecont.Death(); from this script: obstacle collider script

and the death method is on this one:

death script

  • 1
    \$\begingroup\$ Please post code in as text. Images like this can be too small to read on some screens. An guide to the code formatting option can found here. \$\endgroup\$ Apr 12 '15 at 23:14
  • \$\begingroup\$ I could already fix the problem! \$\endgroup\$ Apr 12 '15 at 23:15
  • \$\begingroup\$ what's the error message? \$\endgroup\$
    – jhocking
    May 12 '15 at 20:53

I changed:

gameCont = GetComponent();


gameCont = GameObject.FindObjectOfType();

and worked!

  • \$\begingroup\$ Quick link to FindObjectOfType docs \$\endgroup\$
    – Matt D
    Apr 12 '15 at 23:38
  • \$\begingroup\$ Great help for everybody Matt! \$\endgroup\$ Apr 12 '15 at 23:44

If gamecont is null, then probably the assignment from GetComponent failed which means either the component is not attached to the same GameObject as the one using the ObstacleMove script, or that the instance of GameController is disabled (see the component's check box).

  • \$\begingroup\$ The GameController script is on the main camera and it's enabled, and the ObstacleMove it's on each obstacle. When i create a game object variable and simply put the main camera OBJ in the obstacle prefab to get the gamecontroller script, the prefab doesn't "save" this, and delete this obj added to it. \$\endgroup\$ Apr 12 '15 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.