The use of {get;set;}
Could someone explain what this code below does and why haven't we used set; instead?
private Material curMaterial;
Material material
{
get
{
if (curMaterial == null)
{
curMaterial = new Material(chromeAbbShader);
curMaterial.hideFlags = HideFlags.HideAndDontSave;
}
return curMaterial;
}
}
it's read-only,
so first the person who wrote this makes a private variable of type material, this is the backing field, the person then makes a property with only a get accessor, this means that this is read-only so you cant set the value of it only return the value of it, inside get or set you can use if statement s and such there almost like methods, inside the if statements there is a null check to see if the backing field is null , if it is then it assigns a new material to it, and then it returns the backing field, the reason why he hasnt done this in set is because he wants to make it read-only if he had done it in set then you would have been able to change the value of it but he probably didnt want that.
Answer by dpoly · Nov 07, 2020 at 01:29 PM
This is a getter that creates a default material the first time it is called.