• 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 the_genius · Apr 02, 2015 at 01:09 PM · scripting problemarrayvariableinstancetype

Variable Type for an Instance of any Script

Hello,

I have a group of scripts which all contain a public 'Run' function.

I need to have an array of different instances of these scripts which I can call the 'Run' functions from, but what variable Type do I use?

E.g. I tried using Monobehavour:

 var arrayOfScripts : Monobehaviour[];
 
 public function callScript (scriptNumber : int) {
     arrayOfScripts(scriptNumber).Run();
 }

But, I get the error: 'Run' is not a member of 'Monobehaviour'.

I have also tried Component and Object.

The array needs to be of the instances of the scripts which exist in the scene attached to objects.

I cannot use the script name as it needs to be an array of different types of scripts which have different names.

I could use a js Array, but I would like to be able to drag and drop into the Inspector without having to create a custom inspector.

I can't use an array of GameObjects and use Broadcast Message because two scripts may be attached to the same gameObject and I only want one of them to have 'Run' called.

Any ideas?

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

2 Replies

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

Answer by DoTA_KAMIKADzE · Apr 02, 2015 at 01:31 PM

You should implement Interface.

Here is how to do it in C#, I hope you'll be able to convert it to java yourself:

1) Create a custom script like this:

 public interface ICanRun
 {
     void Run();
 }

2) Add interface to your scripts like this:

 public class RunningPlayer : MonoBehaviour, ICanRun

3)Now declare your array like this wherever you like:

 ICanRun[] runnersArray;

Obviously with all array-related restrictions, so if you want to dynamically populate it would be better to you something like List for example:

 System.Collections.Generic.List<ICanRun> runningList = new System.Collections.Generic.List<ICanRun>();

4)And then in your function just do something like this:

 public void callScript(int iNumber)
 {
     runnersArray[iNumber].Run();
     //same for list
 }
Comment
Add comment · Show 3 · 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 eezSZI · Apr 02, 2015 at 01:39 PM 0
Share

Good call, an interface is probably best.

FYI, some differences between Abstract and Interfaces: http://stackoverflow.com/questions/761194/interface-vs-abstract-class-general-oo

avatar image the_genius · Apr 02, 2015 at 02:30 PM 0
Share

Thanks. I used the unity tutorial: https://unity3d.com/learn/tutorials/modules/intermediate/scripting/interfaces for an interface in js. It works but the interface array does not show up in the inspector.

avatar image DoTA_KAMIKADzE · Apr 02, 2015 at 02:47 PM 0
Share

To be able to see any field in Inspector you need either to make it Public or use [SerializeField], I'd advise to use the second one and use anything but not Public, though it is personal preference.

avatar image
0

Answer by eezSZI · Apr 02, 2015 at 01:27 PM

You could derive them all from a base (and/or abstract) class that has the Run function and then store an array of the base type. (rough C# code below)

 public abstract class BaseRunner : MonoBehaviour
 {
     public abstract void Run();
 }

or

 public class BaseRunner : MonoBehaviour
     {
         public virtual void Run(){}
     }

Then derive all of your other scripts from base class:

 public class ChildClass : BaseRunner
 {
     override void Run()
     {
     }
 
 }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

How do I get unity to tell me what type of variable a variable is? 1 Answer

Adding up instances of a variable across different objects? 1 Answer

Passing Parameters by Value or by Reference 2 Answers

Variable Vs Array? 2 Answers

Vuforia enabled scripting define 0 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