I have some problems with the "static" thinking in C#... It seems it is not legal to access a static method or member variable through an instance.
public FSM fsm;
instance -> fsm.transition();
static -> FSM.SomeMethod();
Question for C# Unity Users - How does one know for example in the Unity Engine which methods or members are static and those that are instantiated?
Answer by CHPedersen
May 24, 2011 at 06:45 AM
Console is actually a public sealed class, but Console.ReadLine() is a static method. C# doesn't strictly support singleton classes in the sense that there's a keyword (like Java's singleton keyword), but you can implement the pattern manually. For those interested, Microsoft explains how in this article.
It's interesting that you should point out the ambiguity between instance access and static access, I'd never realized that, as in your example with A.B(). You see, you're never really in doubt if you use an editor with proper syntax highlighting. Here's an example with a bit of code I zoomed in on, in Visual Studio 2010:
Notice the light blue color? Everything colored light blue is statically accessed. If it's black, it's variable access. I.e. Util.ToolDepthLabelScript.SetTextSize(); is an access to the method SetTextSize() on the variable ToolDepthLabelScript (a script instance), located in the static class Util. The highlighting makes it really simple.
I thought that the code coloring for editors are based on color rules and Code Templates. The members of a class can be either instance members or static members. You access a static member through the name of the class in which it is declared just as you would if it were instance member. So I guess it is not possible to distinguish a static member nor an instance member in the Unity Engine because all class names are highlighted.
Answer by Owen-Reynolds
May 24, 2011 at 06:03 AM
The docs list a var/function as a class function if it is a static, which seems like pretty standard C# convention.
C# really is confusing that way, it's not a Unity thing. In C++, regular functions are probably in a namespace, and you use Mathf::sqrt(9);. The :: lets you know that Mathf is just the scope.
Whereas C# (not just Unity) encourages you to group regular functions in "fake" classes, full of only statics and no member vars, which will never be instantiated. You can never tell in A.B() if A is a variable or a scope. I always assumed it was a design issue: I'm not sure if Console if a singleton or a class, but I use Console.ReadLine() either way.
I should have given a better clarification in order to determine if there was a way to distinguish the difference between the different types of classes and their members. I am fascinated with C#. I behaves like C++ but has a lot of characteristics in Java. With all things considered, the question was not that important.
Answer by ckfinite
May 24, 2011 at 08:00 PM
I normally rely on naming conventions, i.e. fsm is a field, whereas Fsm is a class. What does cause problems is when you have a public property of, for example, a Rectangle that is called Rectangle. In this case, you would have to use either highlighting or looking at statically available functions. VS does make this really clear, IntelliSense is much better than normal rules.
I am trying to get a handle on MonoDevelop as well as everything. I don't want to seem paranoid but it seems that some are inclined lay down misinformation or inclined to lay down principles as incontrovertibly true. Please.
MonoDevelop is much better for C# dev than Unitron/UniScte. You syntax highlighting will work in a similar way to VS's.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
4 People are following this question.
Multiple Cars not working
Distribute terrain in zones
An instance of type "x" is required to access non-static variable "y"
how to acess Static variable in other scripts without extended functions?
cannot implicitly convert type 'int' to 'bool' problem