Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by volkswagenb · Jun 18, 2013 at 07:02 PM · parentclassinstanceaccess

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges