Getter and Setter Question!
Hi Unity Forums! I just stumbled over Csharp getters and setters for the first the ever, as someone used them in a tutorial for creating a healthbar. I have a little bit of background using Java, and c++ so I already understand those getters and setters. Made a calculator etc,etc, nothing huge though.
In One script I have these functions.
public float MaxValue {
get;
set;
}
public float Value
{
set{
fillAmount = Map(value, 0, MaxValue, 0, 1);
}
}
According to a video I watched on this subject get; and set; is the same as get{return MaxValue}, and set{MaxValue = value;}
But then I get a different result. I don't want to copy and paste my whole code unless someone asks for it, as it quite big. Basically, the health does not move.
It seems like these type of getters and setters are used to prevent too many variables. Is this true?
Because I do not want to make another post for such a small question I am going to edit in another question right here:
In one of my scripts I have a variable like this: private Stat health;
I also have the same class public class Stat{}
This class is not attached anywhere (Any object in the game) and that confuses me, yet it still works. What happens when I build the game? So if I have a lots of objects downloaded into my project, and I don't use them are they also just gonna sit there and take up space when I build the game? I thought only what was in the screen was going to be made as game.
getters/setters are a straight C# topic. If you Search non-Unity sources, you'll find all you need to know.
Answer by troien · Apr 15, 2016 at 03:41 PM
This:
public float MaxValue
{
get;
set;
}
Would be the same as this:
private float _maxValue;
public float MaxValue
{
get { return _maxValue; }
set { _maxValue = value; }
}
But it is obviously shorter to write down. (Which is what I believe the only advantage (not 100% sure), the disadvantags being that you can't do anything in the getter/setter yourself)
When you set the inspector to Debug in the right top, you can also see MaxValue his backed field in the inspector. (If you use the get; and set;)
I'm not sure what you mean with 'different result'. As in different result opposed to what? :p
And with the second question, not sure what the question is, but I hope I answer it with this response :p
With not being linked to anything you mean that it's not a MonoBehaviour but just a plain old C# class? Ye you can do that. Note that Serializing them in the inspector can be a pain depending on what you want. But you can use them ofcourse, You can basically use everything from the System namespace of which no class is written with Unity in mind ;). They just are referenced by your scripts, just like string, int, List, etc. are.
How can it be like that because I don't really have any variables :P
I only have these two variables: private float fillAmount; and [SerializeField] private Image Life;
I am actually making a game, and I am trying to make it work so that the healthbar can go up and down. If I changed it to the way I said the healthbar wouldn't go up and down anymore.
Oh, okey so I can make whatever I want in a csharp without mono-behavior and then just initialize them like that. "Scriptname" "variable name". Cool. And then I have to Serialize the fields to be able to see it in the inspector. But does everything in my assets folder go into the game? If I have a prefabs that are NOT used, a model of a character etc, will they go into the game as well? Just like this csharp script without mono-behavior.
As to the first get; set; part, try googling "C# property backing field". That's what they are called. This answer I think explains them well. This one might explain why something about what you said doesn't work as it looks like you did one of those infinite recursions aswell. But I haven't really got any information about what could be 'wrong' as your current question doesn't really cover what the exact changes are you made in code. So situation before and after, not the whole code probably just the things that you changed :p
For the latter part of your question, google is your friend ;) I think this should answer your question.