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 Seyren · Jun 22, 2015 at 09:38 PM · c#constructors

Constructors make my items null

I'm trying to make some new variables, but everytime i try to access them they end up being null, but for some reason in the inspector they show up.

The class is very simple.

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Character
 {
     public string _name;
     public Item[] _wpslot;
     public Item _skill;
 
     bool locked;
 
     public Character()
     {
         _name = "";
         _wpslot = new Item[3];
         _skill = new Item();
     }
 
     public Character(string n)
     {
         _name = n;
     }
 }


Everything goes well except the Item class which ends up null, this is the class:

 using System.Collections;
 using UnityEngine;
 
 [System.Serializable]
 public class Item
 {
     public string _name;
     public int m_id;
     public int m_uid;
     public string m_desc;
     public string _iconpath;
     public int m_resellprice;
     public ItemType _type;
 
     public enum ItemType 
     {
         //Clothing
         Hair,
         Face,
         Accessory,
         Top,
         Bottom,
         Gloves,
         Boots,
         //Offensive
         Weapon,
         Skill,
         //Consumables
         Consumable,
         //Misc
         Miscellaneous
     }
 
     public Item()
     {
         _name = "Empty";
         m_id = 0;
         m_uid = -1;
         m_desc = "Empty";
         _iconpath = "ItemPlaceholder";
         _type = ItemType.Miscellaneous;
     }
 
     public Item(Item i)
     {
         _name = i._name;
         m_id = i.m_id;
         m_uid = i.m_uid;
         m_desc = i.m_desc;
         _iconpath = i._iconpath;
         m_resellprice = i.m_resellprice;
         _type = i._type;
     }
 }
 

And then if i print the Item array for example, it prints null, so i can't access the 3 item array, however everything shows fine in the inspector.

alt text

I even did New Item() just before accessing it and i still got it null, i don't know why this does happens.

Any ideas of why does this happen? Thanks in advance :P

Comment
Add comment · Show 6
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 Dave-Carlile · Jun 22, 2015 at 09:53 PM 0
Share

Don't you have to inherit from $$anonymous$$onoBehaviour or ScriptableObject for your classes to work properly in the editor?

avatar image maccabbe · Jun 22, 2015 at 09:54 PM 0
Share

You make an array of classes but never actually assign objects to the array, i.e.

 _wpslot=new Item[3];
 for(int i=0; i<_wpslot.Length; i++) {
     _wpslot[i]=new Item();
 }
avatar image Seyren · Jun 22, 2015 at 10:52 PM 0
Share

I tried that and it still has the same error, if i try to access the array it throws a null exception.

However, i tried it doing it out of the constructor and it did work, why? i don't know.

             Character c = new Character(name);
             c._wpslot = new Item[3];
             for (int i = 0; i < c._wpslot.Length; i++)
             {
                c. _wpslot[i] = new Item();
             }

Apparently this does work, but the same thing on the constructor does not.

About ScriptableObject, never heard about it, going to check it out.

EDIT: Didn't solve anything unfortunately.

avatar image Bonfire-Boy · Jun 22, 2015 at 11:06 PM 0
Share

@maccabbe is right, though. An exception on trying to access an element _wpslot[] is exactly what one would expect from the code you've shown us, and the solution maccabbe has given should prevent that exception from occuring.

There are lots of other things that could be going on (as well). So, what line of code is actually throwing the exception?

avatar image Bonfire-Boy · Jun 22, 2015 at 11:18 PM 1
Share

You are putting the code that sets things up into the constructor that you're actually using, right?

I just noticed that in your code in the question you're only doing the set-up stuff in the first constructor (the one that takes no parameters), but in the code in your comment you're using the second constructor. The second one (in the code in the question) takes a string as input but then only sets the value of _name.

Try something like this, it removes duplication and makes it harder to forget to add new set-up code to all code-paths...

 public Character()
 {
     init("");
 }

 public Character(string n)
 {
     init(n);
 }

 private init(string n)
 {
     _name = n;
     _skill = new Item();
     _wpslot=new Item[3];
     for(int i=0; i<_wpslot.Length; i++) 
     {
         _wpslot[i]=new Item();
     }
 }

Show more comments

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do you use serialized values at creation? 1 Answer

C# Syntax Question 3 Answers

how to construct all arrays (classes) with default constructor (not possible) 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