Is this not supported or am I just doing it wrong. This crashes unity to the desktop.
public class WEPSystem
{
public int HitCounter
{
get { return HitCounter; }
set { HitCounter = value; }
}
public WEPSystem(){
HitCounter= 4; // <--- error
}
}
not sure it is about that , but why your WepSystem method doesn’t have any return type , at least a void in that case…
now property generally aren’t used more in that way >> (at least for what i know about it …)
public class WEPSystem
{
int hitCounter;
public int HitCounter
{
get { return hitCounter; }
set { hitCounter = value; }
}
public void WEPSystem(){
hitCounter= 4; // <--- error
}
}
well I am not expert tho …so maybe I am saying completely silly thing sorry if it is the case
I think you are correct. I think I was looking at it the wrong way.
I re-read the tut. Properties are a way of encapsulating a field with cleaner syntax. It allows you to seemingly “assign” a value. HitCounter=10; instead of coding the SetHitCounter(10);. It protects the private variable.
Being lazy, coding that extra variable annoys me tx
Going the ‘lazy’ route and just making things public isn’t necessarily a bad way to go about it, but you do have more flexibility when you use properties instead. For one, you can make sure that the value can only be returned but never set by never putting in the ‘Set’ code. Another is that you can perform some extra calculation when the property changes. I have a window class that displays a picture next to a text box and I have a variable that handles what side the picture appears. If I change that variable I will need the text box next to it to recalculate it’s position so it doesn’t obscure the image.
I usually just make the variables public when I am writing the script so I can develop it quickly, and when I am nearing completion of the script I will make some variables available only through properties.
It’s supported, but you’ll probably get an infinite loop, since the Get and Set methods return themselves
Notice that the get and set methods in the following example refer to another variable. It’s debatable whether you need properties in such a simple case, but generally speaking, properties allow you to perform some processing whenever an object’s value is set.
public class WEPSystem
{
private int _hitCounter;
public int HitCounter
{
get { return _hitCounter; }
set { _hitCounter = value; }
}
public WEPSystem()
{
HitCounter = 4;
}
}
For future reference, saying “there’s an error” isn’t nearly as helpful as actually reporting what the error said, or even better copying and pasting the actual error text.