• 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 TheEmeralDreamer · Aug 06, 2011 at 10:50 PM · arrays

Using Arrays Properly in Unity3d

I'm having trouble doing something that should be very simple and thus I'm here. What I'm trying to accomplish is create an array of booleans values.

For instance status ailments Poison=1 Paralysis=0

However every attempt to do so results in errors from the compiler.

var Resistances=new Array();//a list of all current resistances. var Resistances["Weak"]=0; var Resistances["Resist"]=0; var Resistances["Wound"]=0; var Resistances["Paralysis"]=0;

Any help would be greatly appreciated in solving this problem.

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

3 Replies

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

Answer by Bunny83 · Aug 07, 2011 at 12:37 AM

What you need is a Dictionary or Hashtable also known as associative array.

That's actually no arrays in the traditional sense. Keep in mind you're not using JScript in Unity. UnityScript is also a compiled language. Dynamic objects are not supported my .NET / Mono.

In your case i would use a Dictionary. You need to include the "System.Collections.Generic" namespace to use it.

 import System.Collections.Generic;
 
 // ...
 var Resistances = new Dictionary.<String,boolean>();
 Resistances.Add("Weak",false);
 Resistances.Add("Resist",false);
 Resistances.Add("Wound",false);
 Resistances.Add("Paralysis",false);

The values are not auto generated, but once they are added you can use them like an associative array:

 Debug.Log(Resistances["Weak"]);
 Resistances["Weak"] = true;
 Debug.Log(Resistances["Weak"]);


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
avatar image
1

Answer by sven1994 · Aug 06, 2011 at 11:16 PM

Have a look at the scripting reference. You can use arr.Push(object) to add a value to the array. Btw you say boolean... You should use true and false then instead of 1 and 0. Also, leave out the "var" when assigning values.

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
avatar image
0

Answer by SisterKy · Aug 07, 2011 at 07:52 AM

Wiki-post I found very helpful when trying to decide which approach (array, list, dictionary) to use:

http://www.unifycommunity.com/wiki/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F

Greetz, Ky.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Array index is out of range 2 Answers

Can you help me with arrays? 2 Answers

Need help with converting an array to a list in C#` 4 Answers

How Do I Add An Instantiated Object To An Array? 3 Answers

Null reference when accessing GameObject in the Array(C#) 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