This belongs in the scripting section! Documentation is for questions and complaints regarding the docs. Calling @hippocoder for an airlift!
The NullRef happens ebcause there’s no MainClass attached to the same object as OtherClass, so the main variable is a null reference.
The lack of error at main.DoBar is simply due to the line not getting reached as the NullRef terminates the method. If this is just an example and you’re not getting an error somewhere else, please post some example code of that happening
If it’s on a separate game object, you have to get that gameObject so you can use GetComponent. If you use GetComponent on the object you are on, it will only look for Components on that object. You can get that object by either dragging it onto your script from the inspector, or using GameObject.Find().