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 fospher · Jun 26, 2012 at 12:54 AM · getcomponentgame objectgetcomponents

Retrieving a variable from ALL scripts on a Game Object

Hello,

I'm attaching one generic script to several Game Objects - all of which have to retrieve a variable from a script that's not generic and unique only to them. Manually listing each script that I'm going to be using through GetComponent is pretty janky - is there a way that I can, for example, list all Scripts that are attached on a Game Object in an array, and then use a for loop to try to pull out a var? Other methods are welcome as well.

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
1
Best Answer

Answer by whydoidoit · Jun 26, 2012 at 12:57 AM

I can think of two ways:

  1. Define a base class that inherits from MonoBehaviour and has your variable in it, then you can get all of those instances quickly. I could give you an example if I knew what language you were writing in. You then derive your non-generic scripts from this base class. You can then also do GetComponents(YourBaseClass) to get the list.

  2. Use Reflection to find the variable and then get the value. This method is slower and should only be used in initialization code.

Comment
Add comment · Show 5 · 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 Kryptos · Jun 26, 2012 at 11:16 AM 1
Share

As an alternative to creating a base class, you can declare an interface and make your script implement it.

Note that in this case, you cannot declare a variable but must use C# property mechanism.

avatar image whydoidoit · Jun 26, 2012 at 11:17 AM 0
Share

Very good point

avatar image Loius · Jun 26, 2012 at 08:09 PM 0
Share

An example (written freehand, but should compile) -

[Triggerable.js]

 class Triggerable extends $$anonymous$$onoBehaviour {
   var on : boolean;
   function Trigger() { return; } // I forget if return is necessary...
 }

[Lamp.js] - Overrides the base class (triggerable), and has access to its variables

 class Lamp extends Triggerable {
   var light : Light;
 
   function Trigger() { on = !on; light.enabled = on; }
 }

[Other thing] - Looks for triggerables; extending classes can choose what they want 'trigger' to mean, and they all have 'on' available to see

 var trigs : Triggerable[];
 ...
 for ( var t : Triggerable in trigs ) {
   if ( t.on ) t.Trigger();
 }

 var obs : GameObject[];
 ...
 for ( var o : GameObject in obs ) {
   var t : Triggerable = o.GetComponent(Triggerable);
   if ( t && t.on ) t.Trigger();
 }
avatar image fospher · Jun 27, 2012 at 12:41 AM 1
Share

I see what you mean - keeping it all within one class and then extending it in other scripts assigned to the Game Object. However that would mean that all of my scripts would have to be within a class - currently the code is procedural and does not use classes. To avoid having to backtrack, is it advisable to write everything within classes?

avatar image DaveA · Jun 27, 2012 at 01:45 AM 0
Share

Yeah, you'd have to put class wrappers in all those scripts, but that's not a bad idea. This is (one reason) why object-oriented program$$anonymous$$g was invented. If you get in the habit, it makes things easier as you go.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I access a script referenced in another script (without getcomponent)? 5 Answers

[JS] - GetComponents (No appropriate version) 1 Answer

foreach script check if boolean is true 3 Answers

How do i access a variable from another script? 2 Answers

GameObject.Find GetComponent C# 4 Answers


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