Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by castor · Apr 25, 2016 at 06:43 AM · componentreference

How to cleanly reference other scripts inside a script?

In my current project, my scripts often reference other scripts, forcing me to have this at the start of each script:

 private var objManager : Obj_Manager;
 private var globalManager : Global_Manager;
 private var knowledgeManager : Knowledge_Manager;
 private var scriptWorld : Script_World;
 private var phoneManager : Phone_Manager;
 private var worldInteractions : World_Interactions;
 private var sfxManager : SFX_Manager;
 
 function Awake (){
     scriptWorld = GetComponent(Script_World);
     objManager = GetComponent(Obj_Manager);
     globalManager = GetComponent(Global_Manager);
     knowledgeManager = GetComponent(Knowledge_Manager);
     worldInteractions = GetComponent(World_Interactions);
     phoneManager = GetComponent(Phone_Manager);
     sfxManager = GetComponent(SFX_Manager);
 }

As the project gets more complex I wonder, is there a way to simplify this? Is this the normal workflow, or could I have some parent script that references all scripts and they get the references from there?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by LE0NIDAS · Apr 25, 2016 at 07:57 AM

I would guess you always have one instance of a manager running? So you could do under you var declarations something like:

 private GameObject whatever;
 
 
 
 public static MyManager myManager;
 public static MyManager Instance () {
             if (!myManager) {
                 myManager = FindObjectOfType (typeof(MyManager)) as MyManager;
                 if (!myManager)
                     Debug.LogError ("There needs to be one active MyManager script on a GameObject in your scene.");
             }
             return myManager;
         }



In other scripts where you want to use the instance, put in the void Start() method just a:

 myManagerScript = MyManager.Instance ();
Comment
Add comment · Show 1 · 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
avatar image LE0NIDAS · Apr 25, 2016 at 11:54 AM 1
Share

of course this is a c# example, you will have to use c# or see how to rewrite it to javascript, but you should get the idea behind it with this snippet.

avatar image
1

Answer by Overcast · Apr 25, 2016 at 12:47 PM

Have a manager game object in your scene with a script called Manager, also add your other 'manager' classes like an audio manager.

In that class use this:

 public static AudioManager audio;
 
 void Awake()
 {
 audio = GetComponent<Audio>();
 }

Now you can reference your audio manager from anywhere by writing, Manager.Audio.SomeMethod()

Comment
Add comment · Show 1 · 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
avatar image castor · Apr 27, 2016 at 02:37 AM 0
Share

Does this solution cause any performance issues? Some of my scripts only call one or two other scripts and they would now be referencing the 'mega' parent script rather than just accessing two of them. Not to mention that this $$anonymous$$anager. would now be available to any other script.

avatar image
0

Answer by haraldk · Apr 25, 2016 at 02:45 PM

Utilize a framework like StrangeIoc to keep all your structure and refrences clean

http://strangeioc.github.io/strangeioc/

You can than use dependency injection to get references

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

43 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 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 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 avatar image

Related Questions

component not detecting parent class 0 Answers

New UI - Referencing deep nested components on an prefab instantiation 1 Answer

Component refrence goes null randomly? 3 Answers

How to pass reference to variable in IEnumerator method in C#? 2 Answers

Compute Shader Pass RWStructuredBuffer by Reference 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