- Home /
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;
}
}
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.
I was under the impression instantiate was only for creating gameobjects. I am just trying to create an object of a class
Your answer
Follow this Question
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