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 /
avatar image
0
Question by Nuam · May 07, 2010 at 11:02 AM · variablesaccessing

Script access (without script name)

Hello, I need to get all scripts on a gameObject, and then to get a MyVariable in each of them. Something like this:

    function Start(){
var components = GetComponents(MonoBehaviour);
for (var script: MonoBehaviour in components){
 print(script.MyVariable);
}
}

(this gives me errors, of course). I can't use Unity right now, so there can be stupid mistakes in the code, but i hope it helps to understand what i'm trying to do. Thanks in advance!

Comment
Add comment · Show 1
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 MountDoomTeam · Dec 30, 2012 at 08:12 AM 0
Share

I have the same issue, the answer is for C# so I can't use it for UnityScript. The above script should work, why doesn't it?

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Mike 3 · May 07, 2010 at 11:29 AM

your best bet would be to make a parent class with your variable on it and extend that from the other scripts - that way you can use GetComponents(BaseClass) instead, and you have access to the scripts which matter

something along the lines of this , though best to use a seperate js file for each

class BaseClass { var MyVariable : String; }

class MyExtendingClass extends BaseClass { //rest of your normal code here, you can use MyVariable from inside here just fine }

Comment
Add comment · Show 4 · 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 Nuam · May 07, 2010 at 12:18 PM 0
Share

Thank you! I'll try to do it the way you're suggesting, though I'm still learning, so it's still hard to grasp how to use classes in unity. (I'm trying to go through all the scripts on a gameObject, and check $$anonymous$$yVariable, just so i could add any scripts to that object, and as long as they have that variable, they're functions will be called in order i like. Something like behaviors with priority variables.)

avatar image MountDoomTeam · Dec 30, 2012 at 10:22 AM 0
Share

that's difficult! it seems to run okay, but if I try to do something in the class like print something it doesn't seem to happen.

Do you mean that i have to make some kind of Class thingy (sorry) within each of the scripts that is to be switched on or off from the scriptcontrol script? and then it will make a variable that can be accessed in any script? sorry i just put it all in the same script at the moment i didnt understand:

     class BaseClass
     {
     var $$anonymous$$yVariable : String;
     }
     class $$anonymous$$yExtendingClass extends BaseClass
     {


     function Start(){

     var schemeobject = GameObject.Find("schemeobject");
     var components = schemeobject.GetComponents($$anonymous$$onoBehaviour);
     for (var script: $$anonymous$$onoBehaviour in components){
     print(script.name);
     }
     }

     }
avatar image MountDoomTeam · Dec 30, 2012 at 10:58 AM 0
Share

I think at the end of the day this is an error in unity, if you can't make an array of scripts like any other array, because it throws up errors, it makes things difficult for programmers. i ve read all the forum posts on it, and worked for 2 3 hours... $$anonymous$$onoBehaviour[] is not usable.

avatar image Golan2781 · Jan 02, 2013 at 08:49 PM 0
Share

ZoomDomain, you are using print in a class not derived from $$anonymous$$onoBehaviour. This will not work as $$anonymous$$onoBehaviour implements print; your class does not have the function available as it doesn't inherit from $$anonymous$$onoBehaviour.
Use Debug.Log() ins$$anonymous$$d.

Note that the important thing is the type of class you are calling print in. It does not matter that your script variable is cast as type $$anonymous$$onoBehaviour, $$anonymous$$yExtendingClass must be (derived from ) $$anonymous$$onoBehaviour when using print.

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

2 People are following this question.

avatar image avatar image

Related Questions

Adding variables from all scripts 2 Answers

Accessing Other Script Variables (C#) 2 Answers

Accessing other gameobject's script variables : why doesn't this work? 2 Answers

Linking 2 script variables (min + max) to one editor bar - MinMaxSlider? 0 Answers

Accessing Script that Raycast hit 3 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