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 AyooNisto · May 15, 2014 at 07:22 PM · c#dictionarycollections

Dictionary Returning Null Value

I am having trouble using a dictoinary. Whenever i try to get the value it returns null every time. I am alos getting this warning

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.MonoBehaviour:.ctor() GamePiece:.ctor(String, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Int32, Boolean, List`1) WarhammerBoard:loadModels() (at Assets/WarhammerBoard.cs:56) WarhammerBoard:Start() (at Assets/WarhammerBoard.cs:13)

 public TextAsset characterModels;
     private string[] dataLines;
     private Dictionary<string, GamePiece> characterModelsBoard = new Dictionary<string, GamePiece>();
     private List<Weapon> gPWeapon = new List<Weapon>();
     // Use this for initializatio
     void Start () 
     {
         loadModels ();
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     
     void loadModels()
     {
         
         dataLines = characterModels.text.Split('\n');
         string[][] dataPairs = new string[dataLines.Length][];
         int lineNum = 0;
         foreach (string line in dataLines)
         {
             dataPairs[lineNum++] = line.Split(',');
         }
         foreach (string[] line in dataPairs) 
         {
             string modelName = line[0];
             string WS = line[1];
             string BS = line[2];
             string S = line[3];
             string T = line[4];
             string W = line[5];
             string I  = line[6];
             string A = line[7];
             string Ld = line[8];
             string Sv = line[9];
             string Indep = line[10];
             string weapOne = line[11];
             string weapTwo = line[12];
             string weapThree = line[13];
 
             if(modelName == "Hellbrute")
             {
                 characterModelsBoard.Add (modelName, new GamePiece(modelName,int.Parse (WS),int.Parse (BS),
                                                                   int.Parse(S),int.Parse(T),int.Parse(W),int.Parse(I),
                                                                   int.Parse(A),int.Parse(Ld),int.Parse(Sv),gPWeapon));
             }
             else
             {
                 characterModelsBoard.Add(modelName,new GamePiece(modelName,int.Parse(WS),int.Parse(BS),int.Parse (S),
                                                                  int.Parse(T),int.Parse (W),int.Parse(I),int.Parse (A),
                                                                  int.Parse(Ld),int.Parse(Sv),bool.Parse(Indep),gPWeapon));
             }
         }
     }

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class GamePiece : MonoBehaviour {
 
     // Use this for initialization
         string name;
         int weaponSkill;
         int ballisticSkill;
         int strength;
         int toughness;
         int wounds;
         int initiative;
         int attacks;
         int leadership;
         int armourSave;
         int front;
         int side;
         int rear;
         int hullPoint;
         bool independent;
         List<Weapon> gPWeapon = new List<Weapon>();
         
         public GamePiece(string name,int weaponSkill, int ballisticSkill, int strength, int toughness,
                          int wounds, int initiative, int attacks, int leadership, int armourSave, 
                          bool independent,List<Weapon> gPWeapon)
         {
             this.name = name;
             this.weaponSkill = weaponSkill;
             this.ballisticSkill = ballisticSkill;
             this.strength = strength;
             this.toughness = toughness;
             this.wounds = wounds;
             this.initiative = initiative;
             this.attacks = attacks;
             this.leadership = leadership;
             this.armourSave = armourSave;
             this.gPWeapon = gPWeapon;
             this.independent = independent;
         }
         
         public GamePiece(string name,int weaponSkill, int ballisticSkill, int strength, int front, int side, int rear,
                          int initative, int attacks, int hullPoint, List<Weapon> gPWeapon)
         {
             this.name = name;
             this.weaponSkill = weaponSkill;
             this.ballisticSkill = ballisticSkill;
             this.strength = strength;
             this.front = front;
             this.side = side;
             this.rear = rear;
             this.initiative = initative;
             this.attacks = attacks;
             this.hullPoint = hullPoint;
             this.gPWeapon = gPWeapon;
         }
     
 }
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

Answer by Jeff-Kesselman · May 15, 2014 at 07:24 PM

Your error is exactly what the error message says it is.

You cannot create game components with new like you are doing here:

 if(modelName == "Hellbrute")
          {
           characterModelsBoard.Add (modelName, new GamePiece(modelName,int.Parse (WS),int.Parse (BS),
                                                             int.Parse(S),int.Parse(T),int.Parse(W),int.Parse(I),
                                                             int.Parse(A),int.Parse(Ld),int.Parse(Sv),gPWeapon));
          }
          else
          {
           characterModelsBoard.Add(modelName,new GamePiece(modelName,int.Parse(WS),int.Parse(BS),int.Parse (S),
                                                            int.Parse(T),int.Parse (W),int.Parse(I),int.Parse (A),
                                                            int.Parse(Ld),int.Parse(Sv),bool.Parse(Indep),gPWeapon));
          }

You must use Instantiate instead. http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

Because it is erring and throwing an exception, it does not put anything in your dictionary.

And thats why your dictionary is empty.

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 AyooNisto · May 15, 2014 at 07:27 PM 0
Share

I was under the impression instantiate was only for creating gameobjects. I am just trying to create an object of a class

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Sorting a List of Dictionaries in C#? 4 Answers

Flip over an object (smooth transition) 3 Answers

Display random element from dictionary 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