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:


 //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;


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

}//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 :slight_smile:

Let me add, what I’d like would be to access any object as in something like “[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?


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;
  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

      //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

      //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.