I’ve been following this excellent tutorial about loading views via MVC:
I ported the scripts to C#. However, I cannot understand why the inspector shows the Login View for JS, but not for C#. How does one load custom objects/views like this in C#?
If you look at the inspector here, I have added 2 script components to a StartMenu GameObject. One is the StartMenuController.js, and the other is ScriptMenuController.cs (refer to the tutorial link). In StartMenuController, there is a simple public variable for LoginView which simply points to the LoginView script that contains a bunch of GUIStyles and object I want to have appear on screen when told to load. You can see in this pic, the properties of Login View appear in the inspector just fine for JS (top) but no nested properties at all for Login View in C# (it sees the LoginView property itself, but none of its own child properties!)
The LoginView.js script is:
class LoginView implements View {
public final static var NAME : String = "Login";
public var guiSkin : GUISkin;
public var header1Style : GUIStyle;
public var header2Style : GUIStyle;
// ...
Notice the properties that are used (header1Style, etc), that show up fine in the inspector ONLY for JS (not C#).
For C#, there is a slight difference, as it is also implementing MonoBehaviour:
public class LoginView : MonoBehaviour, IView {
public const string NAME = "Login";
public GUISkin guiSkin;
public GUIStyle header1Style;
public GUIStyle header2Style;
//...
Then in StartMenuController.JS, which we associated with the StartMenu GameObject that loads when first run, we have this code:
// Login view:
var loginView : LoginView;
// This function will be called when scene loaded:
function Start () {
// Setup of login view:
loginView.guiSkin = guiSkin;
loginView.header1Style = header1Style;
loginView.header2Style = header2Style;
//...
And in C#, the difference is that unless I put in this gameObject.AddComponent method, I will get a null reference at runtime:
// Login view:
public LoginView loginView;
// This function will be called when scene loaded:
void Start (){
// Setup of login view:
if( loginView == null )
loginView = gameObject.AddComponent<LoginView>();
loginView.guiSkin = guiSkin;
loginView.header1Style = header1Style;
loginView.header2Style = header2Style;
//...
No matter what I do though, the Login View simply doesn’t do a dang thing, and the inspector clearly is not happy. The properties of LoginView only show up with the JS script and not CS! No compiler errors at all - I’ve got the ported code working fine as far as syntax and the ability to compile and avoid runtime errors.
What am I missing?