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.