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 /
avatar image
0
Question by oliver-jones · Mar 13, 2017 at 11:08 AM · arrayclasspopulate

Prepopulate Class Array

Hello,

I've got a class that defines all my game rules called 'GameConstants' (however, they're not actually of the Consts type as they need to be serialized).

For example, I have some ints like:

 public class GameConstants{
    public int BUILD_TIME = 10;
    public int BUILD_COST = 300;
    ///etc...

But I also want to include another class within this class, as an array, and populate it, but I'm not sure how the syntax works for it:

 public BuildingType[] BUILDING_TYPES = new BuildingType[]{
    BuildingType.x = 0,
    BuildingType.y = 0,
    BuildingType.id = 12
 };

This gives me the error 'A field initializer cannot reference the nonstatic field.

How can I pre-populate a class array without using a runtime void? (mono behaviour constructor)

Comment
Add comment · Show 5
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 tinglers · Mar 13, 2017 at 01:35 PM 0
Share

unfortunately I think you'll have to do this during runtime or you'd have to change the class to static.

avatar image oliver-jones tinglers · Mar 13, 2017 at 01:36 PM 0
Share

I am happy to change it to static, like I said - these are game rules, so there will only be one instance of this. Static works for me

avatar image PizzaPie · Mar 13, 2017 at 02:10 PM 0
Share

What do you mean with pre-populate? populate it on inspector? Is BuildingType a monobehaviour?

avatar image oliver-jones PizzaPie · Mar 13, 2017 at 03:28 PM 0
Share

Usually, I would create an empty array, and then in the Start void, I'll populate that array. But I don't want this. I want it so its predefined already, like how you can do with an int array:

 int[] _myArray = new int[]{0,1,2,8}

I want to be able to do that, but with using a class ins$$anonymous$$d. Yeah BuildingType is a mono that I made too.

avatar image PizzaPie oliver-jones · Mar 13, 2017 at 07:54 PM 0
Share

Is there some reason it is a monobehaviour? Any how the way you try to set the values is wrong because it expects either the fields to be static (meaning they are all the same for every instance of the class) or you need to define which instance you want to modify. Now in either case (if it is $$anonymous$$ono or not) the code would be like that:

 buildingTypes = new BuildingType[]
         {
             new BuildingType(0,0,0),
             new BuildingType(1,1,1),
             new BuildingType(2,2,2)
         };

 [System.Serializable]       //no need for that if it is $$anonymous$$ono
 public class BuildingType : $$anonymous$$onoBehaviour
 {
     public int x;
     public int y;
     public int id;
 
     public BuildingType(int x,int y,int id)
     {
         this.x = x;
         this.y = y;
         this.id = id;
     }
 }

The problem is if you run that you ll notice that in the inspector the Array would seem like it is null but actually it is populated and you can access each one of the new BuildTypes you created, also classes that inherit from $$anonymous$$ono don't like the constructor and you ll notice a message "You are trying to create a $$anonymous$$onoBehaviour with the "new"... this is not allowed" fear not the constructor is called any way, but the Start/Update and so on will not be called and that s the reason you don't want it to be a $$anonymous$$onoBehaviour. Lastly to define if you need to be a $$anonymous$$onoBehaviour or not answer the simple question if you ever going to attach it to a GameObject if not and it is meant as a Data container then remove the inheritance.(you could also make it a struct if it does not contain any functions). Cheers for clarifications ask away.

0 Replies

· Add your reply
  • Sort: 

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

75 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 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

Multidimensional Arrays of Classes in Java 2 Answers

Declare new class[].vector3[] length? 2 Answers

"Null Reference Error" when using a custom class as an array 1 Answer

Accessing variables and arrays from inside classes? 1 Answer

how do I set up an array of class vars 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