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 Twayne · May 09, 2011 at 12:05 AM · arrayclass

how do I set up an array of class vars

I set up a class like this

class Level extends System.ValueType{ //fields var levelNo :int; var levelPads :int; // number af pads for level var levelName :String; var levelStatus :int; // editing, testing, done var levelExtra :String; // extras

//Constructor public function SetLevel(lvn:int,lvp:int,lNm:String,lSt:int,lEx:String){ this.levelNo = lvn;this.levelPads = lvp; this.levelName = lNm;this.levelStatus = lSt; this.levelExtra = lEx; }

}

then I declare the var like this

static var CustomLevels :Level = new Level();

Then i want this to be a dynamic array of Levels so I can use

CustomLevels[1].levelName = "Beginers Luck"

I will need to add and subtract to this array so I dont want a builtin array. How can I set this up? I have been getting splicing errors when I add brackets.

EDIT:

That fixes casting as Value type error but I still get 'levelName' is not a member of 'Object'.

static var CustomLevels = new Array(); 
CustomLevels.length = 1; 
CustomLevels[0] = new Level() as Level;// -> Fixed this ERROR 
CustomLevels[0] = InitLevel; 
print(CustomLevels[0].levelName);// -> ERROR
Comment
Add comment · Show 2
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 Eric5h5 · May 09, 2011 at 04:28 AM 0
Share

That's not a class, that's a struct. This sort of thing should probably be a class, so get rid of the "extends System.ValueType".

avatar image Peter G · May 09, 2011 at 11:46 PM 0
Share

I made a very small change that should solve your issues.

1 Reply

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

Answer by Peter G · May 09, 2011 at 04:16 AM

You cannot convert a single instance to an array just by adding brackets like you tried to do hence the error.

I would try a List<>();

import System.Collections.Generic;

static var CustomLevels = List.<Level>();

then you can call Add() and Remove() to add and subtract levels as need be.

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 Twayne · May 09, 2011 at 01:15 PM 0
Share

using pragma strict because of porting to android, unity doesnt like the (). static var CustomLevels : List.; compiles but in run time i get Object reference not set to an instance of an object errors when i reference CustomLevels print(CustomLevels.Count);

avatar image Peter G · May 09, 2011 at 11:47 PM 0
Share

Sorry, it should be an "=" not a ":" in the variable declaration. That should solve the "()"'s problem.

avatar image Twayne · May 10, 2011 at 01:38 PM 0
Share

Alright, you've got me off and running with Lists, thank you.

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

No one has followed this question yet.

Related Questions

Prepopulate Class Array 0 Answers

Cannot edit values of Class when created using .JSON file 1 Answer

Does XML Serialization Support "Class Object" Arrays? 1 Answer

C# Adding to an Array 1 Answer

Resizable Array of Classes? 2 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