1

How to make an inventory with 3 Storage. For example :

if i push storage 1 button it will show 20 Slots at inventory,

if i push storage 2 button it will show from 21 Slots until 40 Slots at inventory, and

if i push storage 3 button it will show from 41 Slots until 60 Slots at inventory.

so it have all 60 slots total.

Below its my code :

inventory.cs

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

 public class inventory : MonoBehaviour {
     public List<GameObject> slotsx = new List<GameObject> ();
     public player Player;
     public List<item> itemx = new List<item> ();
     public GameObject slots;
     item itemxs;
     public int indexofdragitem;
     public Sprite icon;

     int sisa;
     itemDatabase database;
     int totalSlot = 60;
     int currentStorage = 1;
     int view = 20;

     // Use this for initialization
     void Start () {
         Player = new player();
         int slotAmount = 0;
         database = GameObject.FindGameObjectWithTag ("itemDatabase").GetComponent<itemDatabase> ();


         //Generate the Slot and Slot Name;
         for(int i = 1; i <= 60; i++) {
                 GameObject Slot = (GameObject) Instantiate(slots);
                 Slot.GetComponent<slotScript>().slotNumber = slotAmount;

                 slotsx.Add(Slot);

                 Player.items.Add(new item());
                 addChilParent (this.gameObject,Slot);
                 //Slot.transform.parent = this.gameObject.transform;
                 Slot.name = "slot-" + i;
                 Slot.SetActive(false);
                 slotAmount++;

         }
      ShowStorage1();

     }

     //Add Slot Child To GridSlot Game Object
     public void addChilParent(GameObject parentx, GameObject childx) {
         childx.transform.SetParent (parentx.gameObject.transform);
     }
  }

Thanks

Update code :

public void onClickStorage1() {
        HideAllSlot ();
        ShowStorage1 ();
    }

    public void onClickStorage2() {
        HideAllSlot ();
        ShowStorage2 ();
    }

    public void onClickStorage3() {
        HideAllSlot ();
        ShowStorage3 ();
    }

    public void HideAllSlot () {
        GameObject hslot;
        for(int i = 1; i <= 60; i++) {
            hslot = GameObject.Find("slot-"+i);
            hslot.SetActive(false);
        }
    }

    public void ShowStorage1 () {
        GameObject hslot;
        for(int i = 1; i <= 20; i++) {
            hslot = GameObject.Find("slot-"+i).SetActive(true);
            hslot.SetActive(true);
        }
    }

    public void ShowStorage2 () {
        GameObject hslot;
        for(int i = 21; i <= 40; i++) {
            hslot = GameObject.Find("slot-"+i);
            hslot.SetActive(true);
        }
    }

    public void ShowStorage3 () {
        GameObject hslot;
        for(int i = 41; i <= 60; i++) {
            hslot = GameObject.Find("slot-"+i);
            hslot.SetActive(true);
        }
    }
derHugo
  • 83,094
  • 9
  • 75
  • 115
Dennis Liu
  • 303
  • 5
  • 21
  • Are you asking for the logic to create storage having 20 slots. when 1 click the button 1, it will show 1-20, when 2 is click it will show 21-40 and when 3 it will show the 41-60. (button -1)* View +1 => button * View. Assuming button has value 1,2,3. – Miller Jul 01 '16 at 12:10
  • Yes @Miller, But i already got the answer below .. Thanks Miller :) – Dennis Liu Jul 02 '16 at 13:55

1 Answers1

1

"if i push storage 1 button it will show 20 Slots at inventory, if i push storage 2 button it will show from 21 Slots until 40 Slots at inventory, and if i push storage 3 button it will show from 41 Slots until 60 Slots at inventory."

1, click add canvas

2, click to add Button .. in fact add three

3, under Button there is the "text" of the button. Label the three buttons "Storage 1", "Storage 2", "Storage 3"

4, Have a script something like this...

public GameObject yourFIRSTPanel;
public GameObject yourSECONDPanel;
public GameObject yourTHIRDPanel;

private void HideAllThreePanels()
 {
 yourFIRSTPanel.setActive(false);
 yourSECONDPanel.setActive(false);
 yourTHIRDPanel.setActive(false);
 }



public void UserClickedS1()
 {
 Debug.Log("storage 1 needed!")
 HideAllThreePanels()
 yourFIRSTPanel.setActive(true);
 }
public void UserClickedS2()
 {
 Debug.Log("storage 2 needed!")
 HideAllThreePanels()
 yourSECONDPanel.setActive(true);
 }
public void UserClickedS3()
 {
 Debug.Log("storage 3 needed!")
 HideAllThreePanels()
 yourTHIRDPanel.setActive(true);
 }

5, in those three routines, call whatever you want. you seem to already have a script that does ... something. simply have THREE scripts like that, which have the different "amounts" or whatever it is you're saying. simply call the three different scripts from the three different buttons.

Note that you just set all three to be hidden, and then reveal the needed item. It's that easy!


NOTE in your code edit,

   for(int i = 1; i <= 20; i++) {
        hslot = GameObject.Find("slot-"+i).SetActive(true);
        hslot.SetActive(true);
    }

This is wrong: "hslot = GameObject.Find("slot-"+i).SetActive(true);"

Also the whole loop is wrong, should be

  for(int i = 1; i <= 20; i++) {
       String theName = "slot-" + i.ToString();
       Debug.Log("Looking for: " +theName);
       GameObject gFound = GameObject.Find("slot-"+i);
       if (gFound == nil)
        {
        Debug.Log("COULD NOT FIND IT! " +theName);
        continue;
        }
       gFound.SetActive(true);
    }

NOTE HOWEVER THAT !

GameObject.Find does NOT FIND inactive objects!

Note that you are ALREADY KEEPING THEM IN AN ARRAY! (slotsx I think.)

Just use that array!


Finally in fact you should do this:

Group all the slots under three empty gameobjects and just disable/enable those. This way you don't need a loop.

halfer
  • 19,824
  • 17
  • 99
  • 186
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • I have follow you advice, but the recent slot still show up. How to hide the slorage 1 slot if i click storage 2 slot ? Thanks – Dennis Liu Jul 01 '16 at 12:40
  • so basically, use **.SetActive(false)**. Can you search for some examples of that? Also Dennis it is polite to "tick up" an answer which you have commented on and got something from. – Fattie Jul 01 '16 at 12:43
  • Hehee... Sorry.. I forget to "tick up" the question. – Dennis Liu Jul 01 '16 at 13:17
  • let me know if you have trouble with setActive -- I will put in rough example code – Fattie Jul 01 '16 at 13:21
  • @DennisLiu please look at the code i just added as it will help you a lot – Fattie Jul 01 '16 at 13:24
  • Hi Joe, I got trouble adding gameobject Slot with script. How to do that ? so i can set it hide with it. So far i have do is like that a little different from your but actually it is same. Mine is like above.. I have add more code.. – Dennis Liu Jul 01 '16 at 14:20
  • i forget add the error. This is the error : NullReferenceException: Object reference not set to an instance of an object – Dennis Liu Jul 01 '16 at 14:36
  • Hi Joe, if GameObject.Find doesn't FIND inactive object what i should do ? i have try using the array list gameobject : slotsx for example : slotsx[i].setactive(true) the result is same after i have setactive(false) it can't be setactive(true). I have use your code above too. It is same after setactive(false) it cant be setactive(true). – Dennis Liu Jul 01 '16 at 16:20
  • you've just made some simple mistake. edit question, delete your code with GameObject.Find and put on your new code! – Fattie Jul 01 '16 at 16:21
  • Really, do what Joe said, use the array that contains the slots. Doing 20 `Finds` at once might actually cause a little lag and of course it doesn't work for inactive ones. If those functions are in the inventory class it's just `slotsx[i].SetActive(true/false);`. No need of a string. – Gunnar B. Jul 01 '16 at 17:20
  • indeed. put your new code in the qiuestion so we can see it - @GunnarB. will easily help you fix it if you have any minor woes!! do what he says! – Fattie Jul 01 '16 at 17:25
  • Another optimization would be to group all the slots under three storage gameobjects (empty gameobjects) and just disable/enable those. This way you don't need a loop. – Gunnar B. Jul 01 '16 at 21:24
  • YES - op should definitely do that ! – Fattie Jul 01 '16 at 22:22
  • hi @DennisLiu - great! make a link to the other question here, so we can all find it... – Fattie Jul 02 '16 at 12:12
  • Hi @JoeBlow , i got trouble dragging item icon. Would you mind please check this [link](http://stackoverflow.com/questions/38159975/dragging-item-with-icon-following-pointer-unity-c-sharp) . I need a help.. :) – Dennis Liu Jul 02 '16 at 13:51
  • Hi @JoeBlow, Could You please check this link [link](http://stackoverflow.com/questions/38313413/put-ui-image-to-grid-layout-group-unity-c-sharp) I need you help to answer this. Thanks. – Dennis Liu Jul 12 '16 at 02:07
  • Hi @JoeBlow, could you please help me fix this problem. I got trouble on saving the game. This is the link [link](http://stackoverflow.com/q/38473144/6521156) Thanks – Dennis Liu Jul 20 '16 at 06:24