-1

I am getting this error:

NullReferenceException: Object reference not set to an instance of an object Obstacle.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/Scripts/Obstacle.cs:22)

There is no problem in the code. Where am I making a mistake?

[[enter image description here](https://i.stack.imgur.com/sSDkt.png)](https://i.stack.imgur.com/PRHdO.png)

Please help.

Milan Egon Votrubec
  • 3,696
  • 2
  • 10
  • 24
  • Hello and welcome to StackOverflow. Please make sure to check out the guide to ask good questions https://stackoverflow.com/help/how-to-ask . Regarding your error, it's very likely caused by the misspelling of the method `Start` as `start` (mind the capital S) in Obstacle.cs – Eduardo Páez Rubio Nov 22 '22 at 00:22
  • Welcome to SO! As @EduardoPáezRubio stated above, please check out [How to Ask](https://stackoverflow.com/help/how-to-ask). For the sake of everyone who is going to see this question, please *do not* post code/errors/instructions in the form of images. Please copy and paste it into your question and format it properly. – MrDiamond Nov 22 '22 at 00:59
  • If you are getting exception than `There is no problem in the code.` is usually a quite bold statement ;) – derHugo Nov 22 '22 at 10:36

1 Answers1

1

This is an issue caused by a small typo in the form of a lowercase ‘s’ instead of an uppercase ‘S’.

Because the Unity Engine was looking for Start and couldn’t find it, your “start” code never ran. Because of that, your Player was never assigned to, and had a “null reference”. And that’s why when you tried to read the tag, for a reference that was null, you got the error.

Your code should be:

private void Start ()
{
    ..
}

Note the exact case for Start and all C# commands.

Milan Egon Votrubec
  • 3,696
  • 2
  • 10
  • 24