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 Azuri · Jun 23, 2013 at 07:24 PM · variablemonobehaviour

How to assign different Components to one array

Hi

Is it possible to create a Built-in array which contains several classes(MonoBehaviours)? I'm trying to solve this for quite some time now and can't find a solution for this :(

Here is my pseudo Code which shows what I would like to do.

 #pragma strict
 var behaviourClasses : ???;
 
 function test() {
     behaviourClasses = new ???[2]
     behaviourClasses[0] = GetComponent(Attack);
     behaviourClasses[1] = GetComponent(RunAway);
     
     for ( var i: int = 0; i < behaviourClasses.length; i++) {
         if ( behaviourClasses[i].Ready() ) {
             behaviourClasses[i].ready = true;
             behaviourClasses[i].Run()
             break;
         }
     }    
 }

Does someone know how to do this?

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
0

Answer by Em3rgency · Jun 23, 2013 at 08:17 PM

That's the thing, you can't. An array can only store one type of variable. Different kinds of data under one roof is a class. The whole point of classes is that they store many different types of variables and methods that all combine into a singular purpose.

Now what you could do is a master class, and then having all your other classes be children of that class. If your classes have enough similarities between them, that you could place into the parent class instead, and have them inherited, it would work.

As an example unity's GameObject class is a parent to all game scene entities.

Comment
Add comment · Show 12 · 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 Azuri · Jun 23, 2013 at 08:46 PM 0
Share

I've tried to create one $$anonymous$$aster class which contains virtual functions and then override them with different behaviour classes. But I can't get it to work. I always get errors like 'You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword.' Can you maybe show me an example code how to do this?

here is my Class based attempt so far

 // BehaviourClass.js
 
 #pragma strict
 var behaviourClasses : BehaviourClass[];
 function Start() {
     behaviourClasses = new BehaviourClass[1];
     behaviourClasses[0] = new ChaseAndAttack();    
     
     // testing
     behaviourClasses[0].Check();
     // behaviourClasses[0].Run();
     // print( behaviourClasses[0].varTest );
 }
 
 function Check () {
     Debug.Log("Test 1");
 }
 function Run () : IEnumerator {
     Debug.Log("RunTest 2");
 }    


and in another file ...

 // ChaseAndAttack.js
 #pragma strict
 class ChaseAndAttack extends BehaviourClass 
 {
     public var varTest:String = "chase and attack variable";
     
     override function Check() {
         Debug.Log ("Chase and Atack print");
     }
     
     override function Run() {
         Debug.Log ("RunTest before yield");
         yield;
         yield WaitForSeconds(1);
         Debug.Log ("RunTest after yield");
     }
 }
avatar image Em3rgency · Jun 23, 2013 at 08:54 PM 0
Share

The second file looks ok, that IS how you create child classes, with extends.

But the first file... You're still trying to make an array of classes. And then store another class into... wha? Forget arrays all together.

Your ChaseAndAttack class already has all the methods from BehaviourClass, so as long as you make an object of ChaseAndAttack, you will be able to use all the methods inside.

avatar image Azuri · Jun 23, 2013 at 09:16 PM 0
Share

the thing is, I need a function like this >

 for ( var i: int = 0; i < behaviourClasses.length; i++) {
     if ( behaviourClasses[i].Ready() ) {
         behaviourClasses[i].ready = true;
         behaviourClasses[i].Run()
         break;
     }
 } 

I need something like this to check which ai behavior should get started next. I'll have many different Ai behaviors like ChaseAndAttack, RunAway, WalkAround and every behavior is separated in a separate file. every behavior has the same functions (Ready()/Run()). The Script checks if Ready() returns true and if so, it will start Run(), if not it will check the next behaviour for Ready() and so on...

Unfortunately I don't know a different way how to do this :\

avatar image Loius · Jun 24, 2013 at 02:39 AM 0
Share

You can't new a $$anonymous$$onoBehaviour (as the error tells you). That's the only problem. You have to AddComponent the behaviour or use a non-behaviour class.

avatar image Em3rgency · Jun 25, 2013 at 03:30 PM 1
Share

Dude, this IS basic stuff. He creates an object of class DisplayText. The class type is Behavior.

EDIT: sorry, I'm tired, pulled an all nighter. Classes don't have types, but the class inherits from Behavior.

Show more comments
avatar image
0

Answer by moghes · Jun 25, 2013 at 03:39 PM

Since you needed to add them to an Array, use the Array class, check it here and leave comments if you face problems.

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

18 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

Related Questions

accessing vars of other script doesn't work 1 Answer

Get variable from Script in list and , set direct link to other script 1 Answer

How transform Variable is accessed in Scripts ? 2 Answers

script needs to derive from monobehaviour 1 Answer

Monobehavior class variable become null after assigning value when instantiated 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