Are singletons in C# faster than SendMessage/GetComponent?

I’ve read some things on Answers about SendMessage being relatively slow… But recently I found out a cool way to communicate between scripts using singetons, for example:

static public scriptName Instance;

public int doSomething{
    set{
        code goes here
    }
}

I can call this from any script!

I’m making a mobile game right now so performance is important to me

Yes, using a singleton or an event will be much faster than using SendMessage or GetComponent. The reason is that both of the latter options traverse every component in the gameobject in order to find either a method to call (via reflection which is slower than making a method call), or to find he component desired. Two things you can do to avoid this is to, as you suggested, use a singleton, or cache a reference to the component, as such:

SomeComponent cachedComponent = GetComponent< SomeComponent >();

Then you can call a method on that component via:

cachedComponent.SomePublicMethod();

You can also look into an event model to allow several classes to respond to an event that occurs. Please search google for ‘c# events’ for more information. In closing, avoid send message. It’s not only slow, but it’s difficult to follow a code path because it will call a message who h matches a string name on the game object you call it on. There is no way to know which component that method is on, and when others look at your code it’s like a dead end without some manual hunting.