- Home /
The question is answered, right answer was accepted
C# - Best Practice - if null check within property
Dear Community,
I'm not sure if this is the right place to ask this question, so I'll give it a try and hope for enlightenment:
I have this class:
public class BuildOption : MonoBehaviour
{
public static BuildOption selectedOption;
public Texture2D cursorImage;
public void Select()
{
Cursor.SetCursor(cursor, Vector2.zero, CursorMode.Auto);
}
...
}
Whenever any BuildOption is selected via some buttons OnClick event, the Select()
method is called and the Cursor is set to the cursorImage of that selected BuildOption. Now, I was wondering if I should implement a Deselect()
method, where the cursor is set back to the normal cursor. Or whether it would be OK to do the following:
private static BuildOption _selectedOption;
public static BuildOption SelectedOption
{
set
{
{
_selectedOption = value;
if (value == null)
//set cursor back to normal
}
}
}
This seems intuitive because whenever the static property is set to null the cursor has to change. However I didn't know if it is weird to do a check for null within a property setter.
I'd be grateful for your advice. Thank you.
Answer by I_Am_Err00r · Jul 09, 2019 at 03:57 PM
You have it inside a function, that wouldn't work very well or could cause looping issues.
How would the game engine know when to deselect? Is it when you move the cursor outside of a target zone? You need to define that in order for it to work properly.
I could help you, but provide the scenario when you need it deselected and I will show you what I would do.
Ok thanks. I'll try to explain better.
In my scene I have a few buttons. Each button has a different image of a monster that can be built. I call them BuildOptions
.When I click on one button I call the SelectForBuild()
method on the corresponding BuildOption. That method causes the cursor to change to the corresponding cursor image and it starts a coroutine called WaitForBuildCommand()
. This coroutine allows player Input and when some demands are met the monster can be instantiated into the scene.
However if I click the right mouse button I call a method called Deselect()
. That in turn causes the cursor to change back to normal and the Coroutine WaitForBuildCommand()
to stop.
I have another class that also wants to call the Deselect() method. So now I was wondering if ins$$anonymous$$d I could create a static property public static BuildOption SelectedOption
. When that is set to null it causes the cursor to change to normal and the coroutine to stop.
public static BuildOption SelectedOption
{
set
{
_selectedOption = value;
if (value == null)
//set cursor back to normal
//stop coroutine
if (value != null)
//set cursor to value.cursorImage
//start coroutine value.WaitForBuildCommand()
}
}
Does this make any sense? I hope so...
What you had there looks like it would work actually, this is a little cleaner though:
public static BuildOption SelectedOption
{
set
{
_selectedOption = value;
if (value != null)
{
//set cursor to value.cursorImage
//start coroutine value.WaitForBuildCommand()
}
else
{
//set cursor back to normal
//stop coroutine
}
}
}
Hey cool. Thank you very much. So it is not a no-go to do these checks within a property?
It seems so weird, setting a value in a property and then checking for null or not, ins$$anonymous$$d of calling some appropriate method at the right times.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
XMLDocument GetElementByID returning null 1 Answer
Confused as to why I'm getting a NullReference Exception 1 Answer
Have null errors 1 Answer