Could someone checking for my code class if it possible to do like this, I got null inside class value. for detail check below :
classLogBarter.cs // Make a class
[System.Serializable]
public class classLogBarter {
public string DisplayName;
public string PlayerID;
public int CommentID;
public string Time;
public List<item> ItemBarter = new List<item>(); // Here is that possible ?
public classLogBarter (string playerid, string displayname, int commentid, string time, List<item> itembarter // Here how about this ?) {
PlayerID = playerid;
DisplayName = displayname;
CommentID = commentid;
Time = time;
itembarter = new List<item> (); // Is this correct ?
ItemBarter = itembarter; // Is this correct ?
}
public classLogBarter Clone() {
return new classLogBarter (PlayerID, DisplayName, CommentID, Time, ItemBarter);
}
public classLogBarter() {
}
}
LogBarter.cs // Add Value to the class // Asumption that BarterItem Variable Have 1 or 2 Value.
public List<classLogBarter> LogBarter = new List<classLogBarter> ();
public List<item> BarterItem = new List<item>(); // Asumption that BarterItem Have 1 or 2 Value. Here now have BarterItem[0] and BarterItem[1]
LogBarter.Add(new classLogBarter(playerID,displayNames,1,times,BarterItem));
// Debug.Log to show the LogBarter value
for (int i = 0; i < LogBarter.Count; i++) {
for(int j = 0; j < LogBarter[i].ItemBarter.Count; j++) {
Debug.Log ("Player LogBarter : " + LogBarter [i].ItemBarter [j].itemName); // Here there is no value in here.
}
}
What i want to ask is why when Debug.Log LogBarter[i].ItemBarter[j].itemName there is no item ?
Is there anything that i have miss ? or i am just do a mistake in how creating the class with paramater class ?
The error is in classLogBarter
, on constructor.
You are overwriting the value that has those 2 values you are not getting:
itembarter = new List<item> (); // Is this correct ?
Remove that line and you should get the items and not null now.
You are creating a new empty List<item>
but you already done that and filled it with (you said) 2 values, but when entering constructor, you are assigning a new empty one to the same variable that was holding the already filled one.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments