Search Unity

Item List like Inventory

Discussion in 'Scripting' started by Falin, Mar 25, 2012.

  1. Falin

    Falin

    Joined:
    Sep 29, 2009
    Posts:
    242
    hey all,
    I'm trying to make a Inventory system. For now I have:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.  
    6. public class Inventory : MonoBehaviour {
    7.     public int Gold;
    8.     private Vector2 scrollPosition = Vector2.zero;
    9.     static private ArrayList items = new ArrayList();
    10.     static public bool Show = false;
    11.  
    12.  
    13.    static public void AddNew(string name)
    14.     {
    15.      
    16.         //Show = true;
    17.         items.Add(name);
    18.    
    19.     }
    20.    
    21.     void OnGUI(){
    22.         if(Show == true){
    23.          scrollPosition = GUI.BeginScrollView(new Rect (10,10,200,270),scrollPosition, new Rect (0, 0, 200, 400));
    24.          int b = 0;
    25.          int i = 0;
    26.     while(i < items.Count)
    27.     {
    28.         GUI.Button(new Rect(0,b,100,40),items[i].ToString());
    29.         b +=50;
    30.         i++;
    31.        
    32.     }
    33.     GUI.EndScrollView();
    34.     GUI.Label(new Rect(10,280,200,30),"Gold:" +Gold,"box");
    35.         }
    36.  
    37.     }
    38.     void Update(){
    39.         if(Input.GetKeyUp("i")){
    40.             if(!Show){
    41.                 Show = true;
    42.             }else if(Show){
    43.                 Show = false;
    44.             }
    45.         }
    46.     }
    47. }
    48.  
    But now the while statement is called infinite when GUI is displayed. I know thats because OnGUI gets called every frame, but because of that there are more buttons of the same button/item. That's not what I want. So does someone know a other way to add a item and a button to the inventory without getting it duplicated(no loop in OnGUI)?
     

    Attached Files: