- Home /
If I have a bool method, how can I see in the inspector whether it returns true or false?
I know that bool variables can be public or use SerializeField to show up in the inspector, but what about a bool method? How can I see in the inspector whether that method returns true or false?
You can do like this: public class A : $$anonymous$$onoBehaviour { public bool bl=false; void Start() { bl=show(); }
bool show()
{
return true;
}
}
Answer by Mazer83 · Dec 21, 2017 at 04:21 AM
You don't. The inspector can't show you what values methods are returning. If you want to see that, use the debugger. Using monodevelop, place a breakpoint (F9 on the line) where the method is being called, press F5 to connect the debugger to Unity. Run your game in unity with the play button. When the method gets called, the program will halt execution at your breakpoint, and you can step through the lines of code bit by bit with F10. You can hover over variables to see what they are. Press F5 again to continue the program as normal.
I tried to mark your answer down, but apparently I need a reputation of 100 to do that. There are much easier ways of figuring out of the return value other than the one you just explained. I can have the method do a Debug.Log print, or even easier, I can just set a public bool to equal the method.
The debugger is an excellent tool that every programmer should know how to use, Not only is it actually very easy to use, I find it much easier than using Debug.Log to find out what's going on behind the scenes. I don't think what you're asking for is possible with the inspector, which is why I told you about the debugger.
@$$anonymous$$azer83 what sean is talking about is that they can set the value of a public variable to the value of the function inside the update, fixed update, late update, etc functions and then that will update it's value in the inspector for runtime. Do durring runtime yes, the inspector can do what they want just fine. In edit mode it can too, using either [ExecuteInEdit$$anonymous$$ode] or custom inspectors.
Answer by DreadKyller · Dec 21, 2017 at 05:03 AM
Mazer83's answers is also not correct. A standard view of the inspector can't display the value of a function. And to set the value of a public boolean would require some point of entry that updates whenever a change is made. During runtime this isn't a problem, however if you wish to display this value outside of runtime, while editing, then you'll have to deal with custom editors. These are scripts that allow you to manually draw the inspector window for a specific component type, they provide far more powerful functionality for editing, however are more complicated to design. https://docs.unity3d.com/Manual/editor-CustomEditors.html
Alternatively as that page also describes, you can add the [ExecuteInEditMode] to have the script get ran while not playing, depending on the complexity of the script this may or may not be a good option.
Although as for Mazer83's answer, while Debug.Log and setting the value during runtime will work fine for you in this case, it's still a very wise choice to get used to the debugger, because not all problems can be solved with simple logging, and even if a problem can be, often times the debugger is far, far more powerful. People wouldn't have designed debuggers if printing to console solved everything.
"$$anonymous$$azer83's answers is also not correct. A standard view of the inspector can't display the value of a function."
In your first sentence, you say I'm incorrect, but the next one indicates that I was correct; you can't get the return value of a method by looking at the inspector.
Read the rest of my reply please, because you say the inspector can not display values of a function. This is incorrect. the default inspector window can not, Custom Editors Are still inspector windows, they override the rendering of the inspector and they can indeed display values of functions.
You also go on to explicitly say "I don't think it's possible to do this with the inspector" yet while the game is running you can set a variable to the value of the function inside the Start() function. These are two ways that the inspector CAN be used to display the value of a function, they are simply not default. Conflating the inspector in it's entirety, and the functionality provided without additions is where you are running into problems here. You are correct that the standard inspector layout that is generated automatically when no custom editor is found can not display the value of a function by itself, you are wrong that the inspector itself can not be made to display such values when told to.