• 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 ProphetSword · Jul 01, 2013 at 11:48 PM · arraysstaticclasses

Creating a static array of a class

Here's my problem: I'm working on an RPG and I have created public classes to handle characters. These classes include all the information that would be stored on the character (their name, their attributes, life pool, skills, etc) as well as many methods that might be commonly called.

Here's an example of what I mean:

 public class Character {
         
     // Name and Class Information:
     string _name;
     string _classname;
     int _class;
     
     // Attributes:
     int _str;
     int _int;
     int _agi;
     int _wil;
     
 // Character Name:
 public string CharacterName
 {
     get {return _name; }
     set {_name = value; }
 }
     
 // Character Strength:
 public int Strength
 {
     get {return _str; }
     set {_str = value; }
 }
     
 // Character Intellect:
 public int Intellect
 {
     get {return _int; }
     set {_int = value; }
 }
     
 // Character Agility:
 public int Agility
 {
     get {return _agi; }
     set {_agi = value; }
 }
     
 // Character Willpower:
 public int Willpower
 {
     get {return _wil; }
     set {_wil = value; }
 }


I need to store several instances of t$$anonymous$$s class (say, for example, 6) in a static array that can be reached from anywhere wit$$anonymous$$n my game.

I've done t$$anonymous$$s by creating another class to handle it and called it like t$$anonymous$$s:

 public class vrb  {
     
 // Object Variables:
 public static Character[] PC = new Character[4];


When I attempt to use these classes in code, Monodevelop sees them just fine, and I can do t$$anonymous$$ngs like type: vrb.PC[1].CharacterName = "Bob Smith"; and I get no errors.

But, when I run t$$anonymous$$ngs, I frequently get a lot of null reference errors or not$$anonymous$$ng happens at all. So, I'm hoping someone can explain to me what I need to do to make what I want happen or point me to somet$$anonymous$$ng that will explain why it will or will not work.

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 ebogguess · Jul 02, 2013 at 12:09 AM 0
Share
avatar image ProphetSword · Jul 02, 2013 at 08:27 PM 0
Share

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by InfiniBuzz · Jul 02, 2013 at 12:04 AM

Hi

you probably want to create a singleton class.

T$$anonymous$$s would look somet$$anonymous$$ng like t$$anonymous$$s:

 public class Controller : MonoBehaviour {
 
    private static Controller _instance = null;
    public static Controller SharedInstance
    {
       get{
          if(_instance == null)
             _instance = GameObject.FindObjectOfType(typeof(Controller)) as Controller;
          return _instance;
       }
    }
 
    void Awake()
    {
       _instance = t$$anonymous$$s;
       // DontDestroyOnLoad(t$$anonymous$$s)     // if you need
    }

    // if you still want to use an array ;)
    public Character[] PC = new Character[4];
 
 }


then you can access t$$anonymous$$s from everywhere using

 Controller.SharedInstance.PC[...]...

You need to attach t$$anonymous$$s to a GameObject in your scene.

Also make sure you have initiated some characters / filled the array. NullReferenceExceptions can have many sources.

A singleton is also useful for a controller-role through the entire game.

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 ProphetSword · Jul 02, 2013 at 08:30 PM 0
Share
avatar image InfiniBuzz · Jul 02, 2013 at 08:35 PM 0
Share
avatar image InfiniBuzz · Jul 02, 2013 at 09:34 PM 0
Share

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

17 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

Related Questions

Problem setting up an array with a size determined by a variable 2 Answers

Accessing/Initializing array that's part of a class. 1 Answer

multiple classes? 1 Answer

Is there anything wrong with using lots of static classes? 1 Answer

Problems With Variable Class Arrays... 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