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 Olgo · Sep 16, 2013 at 07:15 PM · classreferencevalue

Is this the best way to store my data? It doesn't feel like it.

Hi guys, I'm fumbling my way through an RPG.... I've been hesitant to ask this on here as its kinda big.

Question:

I have a script that creates a class for Sex. In this Sex are strings to call names (his, her, she, him, male, female) and designate the character's gender. Problem is, I can only seem to access these Male and Female Sex classes if I put them in an array and reference their count in the array. See my code below:

 using System.Collections;
 
 public static class SexData {
     
     public static Sex NoGender;
     public static Sex Male;
     public static Sex Female;
     public static Sex[] sexList;
     public static int maleIndex = 1;
     
     public class Sex {
         public string name;
         public string possesiveName;
         public string thirdPersonName;
         public string youngName;
         public string grownName;
         public Vector3 sexScale;
         public int[] hairIndexes;
     }
         
     static SexData(){
         Sex NoGender            =    new Sex();
         NoGender.name            =     "No Gender";
         
         Sex Male                 =     new Sex();
         Male.name                =    "Male";
         Male.possesiveName        =    "his";
         Male.thirdPersonName    =    "he";
         Male.youngName            =    "boy";
         Male.grownName            =    "man";
         Male.sexScale            =    new Vector3(1, 1, 1);
         
         Sex Female                =    new Sex();
         Female.name                =    "Female";
         Female.possesiveName    =    "her";
         Female.thirdPersonName    =    "she";
         Female.youngName        =    "girl";
         Female.grownName        =    "woman";
         Female.sexScale            =    new Vector3(.8f, .9f, .8f);
     
         sexList = new Sex[] {NoGender, Male, Female};
     }
 }

To access these values outside the script, I cant just do "characterSex = SexData.Male", I need to do "characterSex = SexData.sexList[1]"

I cant believe this is the proper way to do what I'm doing, but it works for now. Should I be using a struct? Is this a case of value type vs reference type?

Thanks all!

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

Answer by DaveA · Sep 16, 2013 at 07:37 PM

You should google MSDN dictionary or hash which can let you access things by string.

And/or you can make an enum like Gender { Male, Female } and use that as an index like SexData.sexList[Male]

That said, you code can also be condensed by making a constructor for Sex

Comment
Add comment · Show 1 · 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 Olgo · Sep 16, 2013 at 10:46 PM 0
Share

thanks DaveA, i like your suggestion with the Enum, I'll also add a constructor for Sex.. didn't think of that.

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

16 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

Related Questions

My SendMessage is problematic [SOLVED] 1 Answer

Creating a pointer variable to a GameObject inside a class that does not extend MonoBehavior? [C#] 2 Answers

Enter value in Class array how? 1 Answer

Can I get a reference (not a copy) to a string from a script? 2 Answers

Store reference to array as variable 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