Making sense of static classes.

Sometimes I try and write more efficient code by using static classes, but they always end up throwing errors and I inevitably give up on them and go back to the more clunky way of writing code. I’d really like to push through this mental block so if anyone can clarify things any that would be ace.

Case example.

I have two buttons, and a master controller. Clicking a button sends a message to the controller. The button is also a “Launch Site”.

Selector.js:

#pragma strict
var unselectedMat: Material;
var selectedMat: Material;

function OnMouseDown () {
    
    //Send a function to tell controller to reset all textures to unselected
    
    SelectionManager.ClearAll();
    
    //Send a function to tell controller to use this transform
    
    SelectionManager.ChangeLaunchSite(transform);    
    
    // change texture to indicate selected
    
    renderer.material=selectedMat;

}

static function ResetTex(){
    
    // change texture to indicate unselected
    
    renderer.material=unselectedMat;
}

So the idea is that sends a message to the controller:

SelectionManager.js

#pragma strict
static var launchSite: Transform;

static function ClearAll(){
    
    //Tell all buttons to show unselectedMat material
    
    Selector.ResetTex();
    
}

static function    ChangeLaunchSite(newLaunchSite : Transform){
    
    //set the launchSite variable to be the one instructed by the last button press in the other script.
    
    launchSite=newLaunchSite;    
}

And so what I do is chase error after error, because static classes dont behave like regular ones? Usual trick is to try and add “static” to front of one thing after another but when that hides the var from the inspector I’m screwed.

Presently the last line in the first script throws two errors on the same line.

Assets/Selector.js(25,9): BCE0020: An instance of type ‘UnityEngine.Component’ is required to access non static member ‘renderer’.
Assets/Selector.js(25,27): BCE0020: An instance of type ‘Selector’ is required to access non static member ‘unselectedMat’.

But I need these to be non-static so I can assign to them in the inspector?

Thanks for any help. Would love to get this figured out once and for all. Seems all or nothing using static stuffs.

I would highly recommend reading the MSDN documentation. I know you’re using JavaScript but the same principals apply and will help you understand better what’s going on:

http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.90).aspx

Thanks for that. I mostly follow it. It reinforces my view that static classes are some kind of locked up technique that doesn’t play well with anything that needs public variables. I think I might just stick to not ever using them although I have seen them work elsewhere, really well. Its a tad annoying.

“renderer” is a regular member variable, it is not static. To be accessed from a static class, you would need for “renderer” to be static also.

What @Jaimi said. Static functions of a class belong to the class, not the object that is created. The code in your static function needs to be 100% independent of the objects that get created. You’re trying to use renderer, but since this is a static function, and renderer belongs to an object, the compiler says you made a mistake. :wink:

Thanks for that. So is the idea to have an additional public reference ie public variable and somehow intermingle that?