C# script has no errors in Visual Studio but produces one error in Unity Engine

Hi. I am working on a scoring system for a pinball game, but it will not work. In the Unity engine it produces one error, but when I go to the C# script, there are no errors. Am I doing something wrong? Here is the C# script:

Pinball Script.cs (1.1 KB)

I might be wrong as i am a beginner, but it seems like in line 37, you should replace otherObject.gameObject.name -to- otherObject.collider.name.

So what is the actual error?

It’s very hard to help without the full error message.

1 Like

So looked through the script, looks okay, the main issue may well be no collider2D attached, as for other peoples responses, Spiney199 is correct, so as you do not see an error here is what to do:
Console - there are symbols (triangle etc) click on them, should show the errors, report back what it says. But first double check your script is attached, the colliders are 2D and attached, that all variables are in fact declared in editor (No null values)

can you post a screenshot of the error you are seeing in the editor? its hard to tell from just reading the script

1 Like

First of all. Use code comments! Nobody is going to open your link…

Beyond that, if you have a bug then that only means it’s…

Time to start debugging!!! Here is how you can begin your exciting new debugging adventures!

Debugging can allow you to get all the information you need to discover what the problem is. Once you actually know the problem, only then will you be able to create a solution…

The most coming issue that happens is one of the following:

  • the code you think is running isn’t
  • the code is running sooner or later than you think
  • the code is running less or more often than you think
  • the code is running somewhere else than you think it is
  • you haven’t look at the previous errors or warnings in the console

To help gain more insight into your problem, I recommend literally sprinkling Debug.Log() statements everywhere near the source of the problem to display all the information in realtime.

“When in doubt, Debug.log it out!"

As I said I would, I tested the script, works perfectly fine, issue must be one I stated - No Collider attached or the variables not filled in (your score text etc). Other than that the code for OnTriggerEnter2D works, I get Game Over message, and the OnCollisionEnter2D works, I get Score = 1;

This is the errors I have been getting.

Check your scoreText reference in the inspector. I guess you didn’t drag and drop the ScoreText object to this reference.

That’s the same error twice, caused by the code in lines 19 and 42, and it says that an object reference that should be set is in fact not set. Considering both lines, i.e. scoreText.text = "Score: " + score; are addressing the object scoreText it is that object for which an instance was not designated. To do so click in the Hierarchy window on the game object to which this Pinball Script is attached as to see the component in the Inspector window. Then drag the gameObject (that has the text component and is to show the score in game) from the Hierarchy window to the input field (of your public TextMeshProUGUI scoreText; variable) of the Pinball Script in the Inspector window. And that error should be gone.

1 Like