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 reberk · Oct 09, 2013 at 09:15 PM · arraycomponents

In an array of scripts, how to access their variables?

I've created an array of scripts all attached to the same game object and deactivated. What I'd like to do is to be able to pick three random members of the array and activate those scripts, drawing upon the variables within. However, I run into either a typing error at the outset (thus the untyped declaration here), or, when attempting to enable the script or access a variable, the error that either 'enabled' or 'foo' is not a member of 'Object'. Is there a way to do this cleanly?

 var myArray : Array = new Array();
 
 function Start(){
     myArray[0] = gameObject.GetComponent(Script0);
     myArray[1] = gameObject.GetComponent(Script1);
     myArray[2] = gameObject.GetComponent(Script2);
     myArray[3] = gameObject.GetComponent(Script3);
     myArray[4] = gameObject.GetComponent(Script4);
     myArray[5] = gameObject.GetComponent(Script5);
 
     myArray[1].enabled = true;
 }
 
 function Update (){
     Debug.Log(myArray[1].foo);
 }
Comment
Add comment · Show 5
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 ArkaneX · Oct 09, 2013 at 09:34 PM 0
Share

Do all the scripts have the same variables defined?

avatar image reberk · Oct 09, 2013 at 09:36 PM 0
Share

They do! Just two variables that each script has its own instance of.

avatar image reberk · Oct 09, 2013 at 10:35 PM 0
Share

Ah, thank you to the both of you. Worlds to explore, here. Now, this might warrant a different question entirely, or perhaps I've led us down the wrong path, but does this construction eli$$anonymous$$ate the possibility that each of my three active scripts can provide a different value for the common variable names? Or, in bringing them all together, do they now share a single instance (i.e. the most recently updated)?

avatar image $$anonymous$$ reberk · Oct 09, 2013 at 10:56 PM 0
Share

Each script would be its own instance. BaseScript only means that all of your Script* scripts are of the same base type and thus share an implementation, but it doesn't mean they share the same set of instance variables. So, yes, each script would provide a different value for the common variable names.

I would encourage taking a look at the following links to get a head start on using UnityScript.

http://forum.unity3d.com/threads/34015-Newbie-guide-to-Unity-Javascript-(long) http://wiki.unity3d.com/index.php?title=Head_First_into_Unity_with_UnityScript http://forum.unity3d.com/threads/121178-UnityScript-Inheritance-and-Constructors http://answers.unity3d.com/questions/279809/how-to-perform-explicit-typecasting-in-unityscript.html

Hope this helps.

avatar image reberk reberk · Oct 09, 2013 at 10:59 PM 0
Share

I see. I was feeling pretty confident, but clearly I have to do some reading into inheritance and class definition. I will take your links to heart. Thanks again!

2 Replies

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

Answer by ArkaneX · Oct 09, 2013 at 09:52 PM

If, according to comment, all the scripts have common variables, then create a BaseScript class derived from MonoBehaviour, with your variables. And then make all your scripts derive from this class.

Additionally, don't use Array class. Use either strongly typed array or generic list. For above base class, this would be:

 var myArray : BaseScript = new BaseScript[6]; // you have to declare size

or

 var myList : List.<BaseScript> = new List.<BaseScript>(); // requires importing System.Collections.Generic namespace

If you use list, then you can add elements to it using

 myList.Add(gameObject.GetComponent(Script0));
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 reberk · Oct 09, 2013 at 10:12 PM 0
Share

Following your instructions as best I can (and using list), I keep getting the following error:

 The best overload for the method 'System.Collections.Generic.List.<BaseScript>.Add(BaseScript)' is not compatible with the argument list '(Script0)

I should disclose that I'm completely new to creating my own classes. Somehow I suspect that this is insufficient:

 class BaseScript{
     var foo: boolean;
     var bar: GameObject;
 }
avatar image $$anonymous$$ · Oct 09, 2013 at 10:15 PM 0
Share

$$anonymous$$ake class BaseScript inherit from $$anonymous$$onoBehaviour:

class BaseScript extends $$anonymous$$onoBehaviour { ...

avatar image $$anonymous$$ · Oct 09, 2013 at 10:16 PM 0
Share

Then make each Script* inherit from BaseScript:

class Script0 extends BaseScript {...} class Script1 extends BaseScript {...}

avatar image ArkaneX · Oct 09, 2013 at 10:21 PM 0
Share
 class BaseScript extends $$anonymous$$onoBehaviour
 {
     var foo: boolean;
     var bar: GameObject;
 }

and then you have to derive your normal scripts from this base class:

 class Script0 extends BaseScript
 {
     // content of your script
 }

EDIT: I see Rocket Alien was much faster :)

avatar image
1

Answer by $$anonymous$$ · Oct 09, 2013 at 09:40 PM

Casting your random script instance to a MonoBehaviour may help as each script by default is a MonoBehaviour.

var script = myArray[1] as MonoBehaviour; script.enabled = true;

Is 'foo' a member of all your scripts? You may need to create a base script and make all your scripts inherit from it. Then, cast to the base script type when you access 'foo'.

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

16 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

Related Questions

adding all scripts in current gameobject to an array 1 Answer

Create array of scripts 1 Answer

How to get objects with a specific component and store them in an array. 2 Answers

Adding an array of scripts to a class 0 Answers

Can I make an array of scripts? 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