I got a simple Question and weird from passing value to data type class variable.
the code like below :
void AddItem(int ID) {
for (int i = 0; i < database.items.Count; i++) {
if(database.items[i].itemID == ID) {
itemxs = new item (database.items [i].itemName,
database.items [i].itemID,
database.items [i].itemDesc,
database.items [i].harvest,
database.items [i].itemTime,
database.items [i].stdprice,
database.items [i].hightprice,
database.items [i].itemStock,
database.items [i].Lvlunlock,
database.items [i].rawTree,
database.items [i].itemType,
database.items [i].itemProd,
database.items [i].itemIcon);
Debug.Log ("Item Icon 1 : " + database.items[i].itemIcon); // This appear "Corps/Lemon"
Debug.Log ("Item Icon 2 : " + itemxs.itemIcon); // This appear "Corps/Lemon/Lemon" **even though I just passing value from code itemxs = new item (database.items [i].itemName,...etc)** it is wierd
CheckInventoryExist(itemxs);
break;
}
}
}
itemicon is source from base name : basename + itemName; database.items[i].itemicon contain "Corps/Lemon" The Problem is where i debug.log database.items[i].itemicon it still the same string that is "Corps/Lemon", But when i put "itemxs = new item (database.items [i].itemName,....etc" and then i debug.log itemxs.itemicon it contain "Corps/Lemon/Lemon"
What is going on ?
Thanks
Dennis
Below is the class :
item.cs
using UnityEngine;
using System.Collections;
// Make Class Item
public class item {
public string itemName;
public int itemID;
public string itemDesc;
public string itemIcon;
public GameObject itemModel;
public int itemTime;
public int hightprice;
public int stdprice;
public int itemStock;
public int harvest;
public RawTree rawTree;
public ItemType itemType;
public ItemProd itemProd;
public int Lvlunlock;
private string baseName;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void Awake () {
}
public enum ItemType {
Raw,
Admirable,
Valuable
}
public enum RawTree {
BigTree,
SmallTree,
Field,
None
}
public enum ItemProd {
AnimalBarm,
Mining,
Corps,
Dairy,
JuiceJamMaker,
Merchant,
Kitchen,
Bakery,
CraftHouse
}
public item (string name, int ID, string desc, int harvestx, int time, int stdpricex, int hightpricex, int stock, int Lvlunlockx, RawTree RawTree, ItemType type, ItemProd prod, string folder) {
itemName = name;
itemID = ID;
itemDesc = desc;
harvest = harvestx;
itemTime = time;
stdprice = stdpricex;
hightprice = hightpricex;
itemStock = stock;
Lvlunlock = Lvlunlockx;
rawTree = RawTree;
itemType = type;
itemProd = prod;
this.baseName = folder + "/";
itemIcon = this.baseName + itemName;
}
public item() {
}
}
then itemDatabase.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class itemDatabase : MonoBehaviour {
public List<item> items = new List<item>();
// Use this for initialization
void Start () {
// Add Data To Item List With Class Item
// ------------------------------- Item Corps (Raw - Big Tree) ------------------------------- //
items.Add (new item ("Lemon", 1, "Honey Lemon", 9, 1020, 75, 158, 0, 1, item.RawTree.BigTree, item.ItemType.Raw, item.ItemProd.Corps, "Corps"));
}
// Update is called once per frame
void Update () {
}
}
When I put this code :
itemxs = new item (database.items [i].itemName, // This contain "Lemon"
database.items [i].itemID,
database.items [i].itemDesc,
database.items [i].harvest,
database.items [i].itemTime,
database.items [i].stdprice,
database.items [i].hightprice,
database.items [i].itemStock,
database.items [i].Lvlunlock,
database.items [i].rawTree,
database.items [i].itemType,
database.items [i].itemProd,
database.items [i].itemIcon);
itemName contain : "Lemon";
ItemICon contain: "Corps/" + itemName; You Can check it in itemDatabase.cs file and the constructor is at item.cs.
so the result itemIcon must be : "Corps/Lemon" But
Why itemxs.itemIcon got double itemName ? for example : "Corps/Lemon/Lemon".
In
item
constructor you are doing it:where
folder == Corps
andthis.baseName == "Corps/Lemon/"
anditemName == "Lemon"
so you have exactly what you should get.In your constructor:
Did you get it now??
You may want to assign
itemIcon
value directly instead of doing all this.Do this in your constructor :
Cheers