Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Vioswift · Apr 19, 2016 at 11:04 AM · arrayclassclassesvalues

Unity 3D C# - Making Classes Act Like An Array

So this is my code

 //All the buildings in the game with set values of int
     [System.Serializable]
     public class Buildings { 
         public class nothing { 
             public static int id = 0;
         }
         public class tower { 
             public static int id = 1;
             public static int health = 200;
             public static string resource = "TestCube";
         }
         public class wall { 
             public static int id = 3;
             public static int health = 1000;
             public static string resource = "TestCube2";
         }
     }

Currently to access the variables I must

 string getValue = Buildings.tower.resource;

which I also want. but I want to access the value like an array like this so then I can look up all the values with resources.

this would be an example of what i want.

 string getValueFromArray = Buildings[0].resource;
  string getValue = Buildings.tower.resource;

And the value would be the same either way, is this possible to do? if so, how?

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

Answer by Hrungdak · Apr 19, 2016 at 12:27 PM

All your classes use the same properties, aka id, health, resource.

So make one class

 public BuildingBase {
     public int Id {get;set;}
     public int Health {get;set;}
     public string Resource {get;set;}
     
     public BuildingBase(int id, int health, string resource)
     {
         Id = id;
         Health = health;
         Resource = resource;
     }
 }
 
 Now create a script with a dictionary:
 
 public Dictionary allBuildings;
 
 void Start()
 {
     allBuildings = new Dictionary<string, BuidlingBase>();
     
     allBuildings.Add("nothing", new BuildingBase(0, 0, string.Empty));
     allBuildings.Add("tower", new BuildingBase(1, 200, "TestCube"));
     allBuildings.Add("wall", new BuildingBase(2, 1000, "TestCube"));
     allBuildings.Add("gate", new BuildingBase(3, 100000, "TestCube"));
 }

Now, if you need one of those buildings:

 BuildingBase building = allBuildings("tower");

Comment
Add comment · Show 6 · 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 Vioswift · Apr 19, 2016 at 12:37 PM 0
Share

Not sure what Dictionary is

avatar image Rumor · Apr 20, 2016 at 12:39 AM 0
Share

Use an oredered dictionary, the standard dictionary doesnt maintain order of addition. So if you add a key and object to the dictionary, the position of that key will not necessarily always have the same index.

A dictionary is like a list but you use key words ins$$anonymous$$d of index positions to retrieve the value.

avatar image Vioswift Rumor · Apr 20, 2016 at 03:41 AM 0
Share

I'm trying his method, unity doesn't know what dictionary is, I modified the code, to try to get it to work but no.

 public Dictionary[] allBuildings;
     //Use this for initialization
     void Start () {
     allBuildings = new Dictionary<string, BuidlingBase>();
         
         allBuildings.Add("nothing", new BuildingBase(0, 0, string.Empty));
         allBuildings.Add("tower", new BuildingBase(1, 200, "TestCube"));
         allBuildings.Add("wall", new BuildingBase(2, 1000, "TestCube"));
         allBuildings.Add("gate", new BuildingBase(3, 100000, "TestCube"));
     }
 
     public class BuildingBase { 
         public int Id {get;set;} 
         public int Health {get;set;} 
         public string Resource {get;set;}
         
         public BuildingBase(int id, int health, string resource)
         {
             Id = id;
             Health = health;
             Resource = resource;
         }
     }
avatar image Rumor Vioswift · Apr 20, 2016 at 03:46 AM 0
Share

Currently at work so will take a look when I get home, for now try read up here about ordered dictionarys.

You will have to import the system.collection.special

https://msdn.microsoft.com/en-us/library/system.collections.specialized.ordereddictionary(v=vs.110).aspx

Show more comments
avatar image Hrungdak · Apr 20, 2016 at 05:16 AM 0
Share

The declaration of the Dictionary should be

public Dictionary<string, BuildingBase> allBuildings;

And you need System.Collections.Generic as using directive.

avatar image
0

Answer by giorashc · Apr 19, 2016 at 11:19 AM

You can define in a script an array property of the type Building :

 public class MyScript : MonoBehaviour {
 
     public Building [] buildings;  
 }

You will need to initialize the array in the inspector and then you can easily access it as in the example you provided

Note that the members in the Building inner classes are static and should be changed to be non-static (otherwise they will be the same for all building instances)

Comment
Add comment · Show 4 · 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 Vioswift · Apr 19, 2016 at 11:57 AM 0
Share

I get errors like Assets/Scripts/TerrainSetup.cs(47,74): error CS0120: An object reference is required to access non-static member `TerrainSetup.Buildings.nothing.id'

avatar image giorashc Vioswift · Apr 19, 2016 at 12:09 PM 0
Share

Did you initialize the array of buildings in the inspector? can you show the code where you try to access this array?

avatar image Vioswift giorashc · Apr 19, 2016 at 12:13 PM 0
Share

I did this. I'm not sure what u mean by " in the inspector" sorry.

 void Start () {
         print (buildings[1]);
     }
 
     public Buildings[] buildings;
     //All the buildings in the game with set values of int
     [System.Serializable]
     public class Buildings { 
         public class nothing { 
             public int id = 0;
         }
         public class tower { 
             public int id = 1;
             public int health = 200;
             public string resource = "TestCube";
         }
         public class wall { 
             public int id = 3;
             public int health = 1000;
             public string resource = "TestCube2";
         }
         public class gate { 
             public int id = 4;
             public int health = 10000;
             public string resource = "TestCube2";
         }
     }
Show more comments
avatar image
0

Answer by Rumor · Apr 19, 2016 at 01:22 PM

Buildings is a class, so you would have to create an instance of the class. As im assuming you are wanting multiple Buildings Instances, or are you going to use Buildings as a singleton or container for buildings?

string getValueFromArray = Buildings[0].resource; The above line of code would assume that the variable Buildings is of list type but you actually want it to be the Class. Im not sure is that is actually achievable.

I would suggest creating a container class that has the following code.

using System.Collections.Generic; public List buildings = new List()

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

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

Related Questions

(SOLVED) Accessing (string) arrays by enumerations from another class 0 Answers

[SOLVED] Increase and Decrease size of Class Array in Unity C# 1 Answer

C# Find specific object by getting one of its variables 0 Answers

Accessing scriptable object classes class from other script 1 Answer

How to reference a class in an enumerator 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