[solved] I broke my game, tried to reassign scripts now nothing works

In a previous post I pointed out a problem with maximum number of guesses taking its value from where it was set in Unity and NOT from the value in the script. I removed the script link to the empty in game and reassigned it and sure enough THAT number was placed into the game but now THE SCRIPTS DON’T WORK. How do I fix this mess? For example pressing GuessHigher button causes an error and values aren’t being passed to the scripts ->

and the line th error message points to in the script worked fine before I detached the script and then re-attached it to the empty _>

Is there a way to update links in the Unity editor? How can I get the script and buttons to work again other than do the entire game all over from scratch?

can you not just click the buttons and update the script? Nothing should be stopping that

The null reference exception error on line 28 is pointing to your text variable, the one of type Text - this is the Text object which you would drag from the hierarchy into your exposed variable on the script component. You haven’t popped a screenshot of that up so I can’t tell if you have done that but I am guessing not.

If you select the NumberWizardGame game object in the hierarchy, make sure the Inspector is displayed and then pop a screenshot up that would be handy.

There’s an unnecessary semi-colon on line 33 also. Beyond line 34 it’s hard to tell as it’s not include in your screenshot, but it appears there are comments, any further code? If so, it would be outside of the class.

Better to paste the code in to your post really and use the Preformatted text button (and some spaces if necessary) to tidy it up.

1 Like

YES, thank you, that was it, I forgot to put the variable Guess into text field.

BUT QUESTION: how do we make the variable maxGuessesAllowed use the value in the script and not from the field in the Unity Editor?? Is that not a good idea?

If anyone cares to see the error, I fixed it in this screen shot -> this is where the game script is attached to the empty and at the end of where you see script in the Inspector on the right side, you see the field text and I had to enter the variable name ‘Guess’ for it to pass back to the script for the script to have something to process.

Thanks for the help, I tried that but it turns out I needed to put the variable name Guess into the text box. Still a bit shaky about what goes into the editor and what stays in the scripts. I’m pretty sure I took it out when trying to debug and I now know that everything the editor leaves for you to fill in may need some attention!

They are one in the same. You are exposing the variable to the Unity Editor by having it as a public class level variable (field ).

If you dont want it to appear in the Unity Editor, change public to private.

1 Like

Public fields exposed to the editor are mainly useful for configuration:

  • values that need to be playtuned, e.g. speeds
  • values that will be different with different instances of the script/prefab (e.g. configuring a different sprite for each)
  • storing references to other objects in the hierarchy that will need to be referenced by the script, especially where performance is a concern (as GameObject.Find is a heavyweight operation)

Private fields and variables are more for things that will change at runtime.

2 Likes

Privacy & Terms