• 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 Deculsion · Jul 01, 2015 at 09:57 AM · c#scripting problemaddcomponent

Getting invalid rank specifier when adding component from array

I'm trying to create a function which returns a gameobject with random scripts attached to it. What I'm trying to do is to get the scripts via prefabs and using AddComponent into them with this script.

 using UnityEngine;
 using System.Collections;
 
 public class CreateRandom : MonoBehaviour {
     public GameObject EnemyShell;
 
     public ~~GameObject[]~~ MovementArray; //All these arrays shouldn't be of GameObject type. I made a mistake. It should be some way of getting different script names (Movement0,Movement1 etc.) into an array)
     public ~~GameObject[]~~ PatternArray;
     public ~~GameObject[]~~ BulletArray;
 
     private GameObject NewEnemy;
 
     GameObject CreateEnemy(){
 
         NewEnemy = EnemyShell;
 
         NewEnemy.AddComponent<MovementArray[Random.Range(int 0, MovementArray.Length-1)]>();
         NewEnemy.AddComponent<PatternArray[Random.Range(int 0, PatternArray.Length-1)]>();
         NewEnemy.AddComponent<BulletArray[Random.Range(int 0, BulletArray.Length-1)]>();
 
         return NewEnemy;
     }
 }

However with this script, I'm getting the error "Invalid rank specifier: expected ',' or ']'" when I try to use the AddComponent method.

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
0

Answer by Graham-Dunnett · Jul 01, 2015 at 10:20 AM

GameObject.AddComponent is documented as adding a script to a GO. Your MovementArray is an array of GameObjects. You cannot add a GO as a component. I think you are suggesting that the GOs in the MovementArray all have different scripts applied. So, you'll need to use GetComponents to get a list of the components on the GO. So, something like (not compiled, might have bugs, but you get the idea):

 GameObject movementGO = MovementArray[Random.Range(int 0, MovementArray.Length-1)];
 A[] a = movementGO.GetComponents<A>();
 B[] b = movementGO.GetComponents<B>();
 C[] c = movementGO.GetComponents<C>();
 
 if (a.length > 0)
     NewEnemy.AddComponent<A>();
 if (b.length > 0)
     NewEnemy.AddComponent<B>();
 if (c.length > 0)
     NewEnemy.AddComponent<C>();


where your script components are called A, B and C. I suspect they have a more descriptive name (which will be the name of the class in your c# scripts.)

Comment
Add comment · Show 1 · 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 Deculsion · Jul 01, 2015 at 12:00 PM 0
Share

Oh wow I didn't realise I used GameObject[] for the array. I was actually intending to be able to drag a script into the editor like referencing a normal game object, rather than creating a prefab for each script.

The method you proposed sounds like a good alternative, though I don't quite understand the purpose of having multiple arrays. I kinda forgot to add context, but what I'm trying to do is to create a script that will attach a random script from each array onto the NewEnemy GameObject and return that.

From what I understand of your code, is it taking all the scripts in the GO and adding all of them to NewEnemy? Because I'm trying to just get one script from each array.

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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Set default length for an array of elements of a custom class in inspector 0 Answers

How to rotate a projectile to face the player in 2D? 1 Answer

How do I make a variable of type C# class? 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