- Home /
Class instance access to scene?
Hello all, I've looked around and just haven't found a post on the following:
I have a "camera director" class, that controls several cameras for my game, with different behaviors. However, I haven't been able to give this class' instance access to the scene so it can base those behaviors on the parent scene/script object positions, etc.
I have two separate files, one called "MAIN.js" (tied up to a dummy scene), for centralized control of the game, and one called "camera_director.js" where the camera controller class resides, as follows:
MAIN.js-------------------
//CAMERAS
//we have our own camera director controller to obtain further
//behaviors not provided by Unity.
//
var camera_director:camera_director;
function Awake () {
DontDestroyOnLoad (transform.gameObject);
}//
function Start () {
camera_director =new camera_director();
camera_director.main = this; //<have tried this with no success
}//
function Update(){
var t:float=Time.deltaTime;
camera_director.update(t);
}
camera_director.js-------------------
class camera_director{
var main:MAIN; //<<tried this as a "pointer" initialized in MAIN.js
//var main:GameObject; //<<tried this too
function camera_director(){//constructor
//main = GameObject.Find("MAIN");//<<also tried this unsuccessfully
}//
function update(t:float){
main.some_variable=0; //test <<NO SUCCESS ACCESSING OBJECT IN PARENT SCRIPT
//cameras[1].look_at(main.object.position);
}
}//end class
Sorry if it's dumb to ask this, but how do you get a "pointer" to the parent script? So far this keeps giving me a "null reference" exception, clearly showing the pointer I'm setting is invalid.
The halfassed way to work around this would be to dump the controller code as classless in the main file, but that would be really mmh "classless".
I'd really appreciate any hint any one can provide :)
Edit: Let me add, what I'd like would be to access any object as in something like "main.car[1].position" without using the "GameObject.Find" for every single item. But now that I think of it, it seems that's the only way in Unity JS, can someone please confirm?
Thanks.
Answer by volkswagenb · Jun 28, 2013 at 09:03 PM
Ok here's what you do.
Again, my 'main' is a dummy object in a dummy scene #0. and my 'main script' is attached to it. My stand-alone class then sets up a reference to the main script in the following way:
class camera_director{
var m:GameObject;
var main:Component;
//
//states
var state:int=0;
var state_time:int=0;
var STATE_INIT:int=0;
var STATE_NORMAL:int=1;
function update(t:float){//<<my own update, manually called from main script
if(state==STATE_INIT){//----------
//Get reference to parent game main script.
//Only once everything is running
//(i.e you can't put this in the class constructor)
m = GameObject.Find("MAIN"); //dummy game object.
main = m.GetComponent("MAIN_script"); //script component of it
change_state(STATE_NORMAL,0);
//
}
if(state==STATE_NORMAL){//------------
//Once we have the reference to main, we can do whatever.
//It's a good idea to check if an object is null before
//accessing it though.
//...
}//
}//end class
In this application, this allows my camera director access to my cars' positions in order to follow them, etc.
I hope this helps someone.
Your answer
Follow this Question
Related Questions
How can I access a function without knowing the script/class name to which it belongs? 1 Answer
Serialization - Variables won't change on original construction 1 Answer
How can I access inspector-set variables through a class? 1 Answer
Can't get script to access other script/class (c#) 1 Answer
making my classes usable? 1 Answer