I’m learning some C# and Unity and came across this tutorial where I thought the creator did something strange.
If we want to get a value from another file, which is better?
Example:
This would be the file where we update the variable we want to access.
public class Player : MonoBehaviour
{
public bool isActive;
if (reason) {
isActive = true;
} else {
isActive = false;
}
public bool IsActive() {
return isActive;
}
}
Which option would be better if we wanted to access the “isActive” variable in another file?
Accessing the variable?
bool isActive1 = player.isActive;
Or Accessing the function?
bool isActive1 = player.IsActive();
Both obviously work. I want to know which is better and for what reason.
In my head accessing the variable is cleaner and I don’t understand why using a function would be faster or more efficient.
Maybe when working with a bigger project using the function will be better.
I’m not sure.
Bugsie71 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4