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 davidwalmsley · Jan 27, 2013 at 04:41 AM · arrayclass

Adding scripted variables to an array

I am trying to set up way for the player to select spells during combat. I need to put them into an array so that I can I list them and scroll through them. Being a newbie to Unity Script I thought the best way to go about doing it was make a base class which would be Spell, and create my spells either by extending the spell class or just creating them from the class in a spell manager class.

So I was thinking my array would be:

 var spells : Spell[];

and then add stuff like this:

 fireball = new Spell("Fireball");
 
 spells.Add(fireball);

And I get an error thats says type has to be derived from UnityEngine.Object. Type is Spell.

Are there any solutions to make my array work correctly? And if I'm going about it all wrong, how would I get an array of abilities?

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
1
Best Answer

Answer by Eric5h5 · Jan 27, 2013 at 04:58 AM

You cannot add to built-in arrays like that; they are a fixed size. Initialize the array to the desired size and do something like

 spells[0] = fireball;

If you need the array to be a flexible size, use a generic List instead.

 import System.Collections.Generic;

 var spells : List< Spell >;
Comment
Add comment · Show 2 · 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 davidwalmsley · Jan 27, 2013 at 07:26 AM 0
Share

Lists is definitely what Im looking for, thanks. I have run into a new problem now.

I am trying to pass information from one list to another, which I managed just fine when done from a single script :

 import System.Collections.Generic;
 
 var myAbilities : List.<Ability>;
 var myList : List.<Ability>;
 
 function Start(){
     fireball = new Ability(Fireball());
     blizzard = new Ability(Blizzard());
     
     myList.Add(fireball);
     myList.Add(blizzard);
     
     for(var thing in myList){
         AddAbility(thing);
         
     }
     
 }
 
 
 
 function Update () {
 
 }
 function AddAbility(otherThing : Ability){
     myAbilities.Add(otherThing);
 
 
 
 }

The items from the first list popped over onto the other one with no problem, but when I am trying to retrieve the information from a list in a different script it wont read it. I attached this script to the same game object and tried to get the info from one of the lists from above:

 import System.Collections.Generic;
 
 var dude : Baddie;
 var spells : Blah;
 var abilities : List.<Ability>;
 
 function Start(){
     spells = GetComponent(Blah);
     dude = new WhiteOrb();
     
     for(var thing in spells.myAbilities){
         RoundEmUp(thing);
         
     }
 
 }
 
 function RoundEmUp(someThing : Ability){
     abilities.Add(someThing);    
 }

and the new list reading empty. I have tried it in a bunch of different ways, but I cant seem to get the information from a list in another script to transfer over. Am I missing something?

avatar image Eric5h5 · Jan 27, 2013 at 09:54 AM 0
Share

If you have two scripts with a Start function, the order in which they run is undefined, unless you use the script execution order project settings. Or use Awake in one script and Start in another.

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

10 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

Related Questions

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

Calling functions from a class - Curious issue 1 Answer

Accesing custom class object from another script 1 Answer

How to increase the size of a class array? 4 Answers

Level generation using a "tree" type structure, why when i add a cell i get an error argument out of range? 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