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 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 this class (say, for example, 6) in a static array that can be reached from anywhere within my game.

I've done this by creating another class to handle it and called it like this:

 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 things like type: vrb.PC[1].CharacterName = "Bob Smith"; and I get no errors.

But, when I run things, I frequently get a lot of null reference errors or nothing 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 something 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

Are you initializing each character in the PC array in your vrb constructor? You need to make sure that they are not null before you start assigning things to them. The compiler won't know that they will be null, so you'll find those errors at run time.

avatar image ProphetSword · Jul 02, 2013 at 08:27 PM 0
Share

Frequently, one of the errors I get doesn't have anything to do with it being null, but about it not be attached to an instance of an object. I didn't think that was a requirement for scripts that don't inherit anything from $$anonymous$$onoBehaviour.

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.

This would look something like this:

 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 = this;
       // DontDestroyOnLoad(this)     // if you need
    }

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


then you can access this from everywhere using

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

You need to attach this 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

Singleton class? Does that mean that everything is contained within the class and that it instantiates from within itself? I'm confused, but if anyone can point me to some information about them, that would be awesome. I'm sure I could figure it out. Thanks for the help so far.

avatar image InfiniBuzz · Jul 02, 2013 at 08:35 PM 0
Share

It basically means you only have one instance of this class and you can access it from everywhere. This is good for controllers because you can be sure you wont have your data spread on multiple instances and have easy access.

Im on mobile bit can provide you a link asap. Or just google it ;) theres a good page on the wiki and also some others ;)

avatar image InfiniBuzz · Jul 02, 2013 at 09:34 PM 0
Share

here's one of them: Singleton - Unify Community Wiki

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

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

adding classes to arrays 2 Answers

How to list all the properties and methods from the class? 1 Answer

Customize arrays and classes in inspector window 2 Answers

Can I preserve the values in a parent class when I overwrite it with a child class? 1 Answer

Inventory Help. 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