0

I got a simple question to ask. Just Check below code and the question is in the end at the result of code. I hope you guys can help me..

file inventory.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class inventory : MonoBehaviour {
    public List<item> tmp = new List<item> ();
    public item itema;


    itemDatabase database;

    // Use this for initialization
    void Start () {
        int slotAmount = 0;

        database = GameObject.FindGameObjectWithTag ("itemDatabase").GetComponent<itemDatabase> ();
        //Generate the Slot and Slot Name;
        for(int i = 1; i <= 24; i++) {
                tmp.Add (new item());   
        }

        itema = database.items [0];
        tmp [0] = itema;
        tmp [0].itemStock = 1;
        Debug.Log (tmp[0].itemID + " - " + tmp[0].itemStock);
        itema = database.items [0];
        tmp [1] = itema;
        tmp [1].itemStock = 2;
        Debug.Log (tmp[1].itemID + " - " + tmp[1].itemStock);
        itema = database.items [0];
        tmp [2] = itema;
        tmp [2].itemStock = 3;
        Debug.Log (tmp[2].itemID + " - " + tmp[2].itemStock);
        Debug.Log (tmp[0].itemID + " - " + tmp[0].itemStock);
        Debug.Log (tmp[1].itemID + " - " + tmp[1].itemStock);


    }



}

and the result was :

tmp Array [0] : 1 Stock 1

tmp Array [1] : 1 Stock 2

tmp Array [2] : 1 Stock 3

tmp Array [0] : 1 Stock 3 // this array recently stock was 1 now replace with 3 Why it is replace i have use an array ?

tmp Array [1] : 1 Stock 3 // this array recently stock was 2 now replace with 3 Why it is replace i have use an array ?

The Question is on tmp Array[0] and tmp Array[1] it was replace by tmp Array[2].

THanks Dennis

1
  • Please remove unnecessary codes, then you will get better response Commented Jun 28, 2016 at 3:33

1 Answer 1

1

That is because in C# itema = database.items [0]; will not create a copy but rather itema will always point to object. So what is happening is you are not adding duplicates to your array but rather all the things point to same element that is the value of database.items[0]. Which in turn is getting modified.

I would rather suggest you to create a new class instance like this

Set database.items[0] values in items

itema = new items( database.items [0].name, database.items [0].ID, ....)

then assign this itema tmp [0] = itema;

remember in c# the varibles just point to the object and will not create duplicate when you try to assign it. You will need to manage that.

Sign up to request clarification or add additional context in comments.

6 Comments

Hi killer_mach, i have follow your advice to use <b>itema = new items( database.items [0].name, database.items [0].ID, ....)</b> but i got error like : <i>The type item' does not contain a constructor that takes 12' arguments</i> . I already have the constructor at item.cs.. What Should i do ?
share your class in above question we can have a look at that
i went in your old edit history first of all your class has 13 parameters 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) { } so how are you expecting it to take 12 parameters
please see the error. It is very basics of programming. You just need to add 13 parameters or create another constructor which accepts your required no of parameters. You just have 12
I am glad it worked. If you think this worked please mark answer as solution.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.