Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need Help!!: Generating Game Objects from Array and Item List

Discussion in 'Scripting' started by dylfahr, May 4, 2015.

  1. dylfahr

    dylfahr

    Joined:
    Apr 30, 2015
    Posts:
    12
    I am having difficulties spawning the gameobject represented in one array, by clicking on icons that resemble the gameobject and have similar id numbers. I want it to function where I can draw up the inventory, chose any of the 4 items, and spawn the selected game object. So far it is only generating the base value in the array, the first game object in value 0. How do I go about spawning other game objects depending on user's input/ clicking and dragging of the icons?

    here is the code for the items:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. [System.Serializable]
    7. public class item {
    8.     public string itemName;
    9.     public int itemID;
    10.     public string itemDisc;
    11.     public Texture2D icon;
    12.  
    13.  
    14.  
    15.     public item(string name, int id, string desc){
    16.         itemName = name;
    17.         itemID = id;
    18.         itemDisc = desc;
    19.         icon = Resources.Load<Texture2D> ("Icons/" + name);
    20.    
    21.  
    22.     }
    23.     public item(){
    24.  
    25.         }
    26.  
    27. }
    the code for the item database:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class itemDatabase: MonoBehaviour {
    6.     public List<item> items = new List<item>();
    7.  
    8.     void Awake(){
    9.         items.Add (new item ("Cube", 0, "The newest IKEA cube"));
    10.         items.Add (new item ("Sphere", 1, "It's Spherical... SPHERICAL"));
    11.         items.Add (new item ("Column", 2, "The finest Persian Column"));
    12.         items.Add (new item ("Robot", 3, " Not sure...."));
    13.     }
    14. }
    and the inventory:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Inventory : MonoBehaviour {
    6.     public int slotsX, slotsY;
    7.     public List<item> inventory = new List<item>();
    8.     public List<item> slots = new List<item>();
    9.     private bool showInventory;
    10.     private itemDatabase database;
    11.     private bool showToolTip;
    12.     private string toolTip;
    13.     private bool dragItem;
    14.     private item draggedItem;
    15.     public GameObject[] items;
    16.     private itemPlacement ItemPlacement;
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.         ItemPlacement = GetComponent<itemPlacement> ();
    21.         for(int i = 0; i < (slotsX * slotsY); i++){
    22.             slots.Add(new item());
    23.             inventory.Add (new item());
    24.         }
    25.         database = GameObject.FindGameObjectWithTag ("Item Database").GetComponent<itemDatabase> ();
    26.         addItem (0);
    27.         addItem (1);
    28.         addItem (2);
    29.         addItem (3);
    30.  
    31.     }
    32.     void Update(){
    33.         if(Input.GetButtonDown("Inventory Menu"))
    34.            {
    35.             showInventory = !showInventory;
    36.         }
    37.     }
    38.    
    39.     // Update is called once per frame
    40.     void OnGUI () {
    41.         toolTip = "";
    42.         if(showInventory)
    43.         {
    44.             drawInventory ();
    45.         }
    46.         if (showToolTip){
    47.             GUI.Box (new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 200, 50),toolTip);
    48.  
    49.             }
    50.         if (dragItem){
    51.             GUI.DrawTexture (new Rect(Event.current.mousePosition.x, Event.current.mousePosition.y, 50, 50),draggedItem.icon);
    52.         }
    53.  
    54.     }
    55.     void drawInventory(){
    56.         Event e = Event.current;
    57.         int i = 0;
    58.         for (int x = 0; x < slotsX; x++) {
    59.             for (int y = 0; y < slotsY; y++) {
    60.                 Rect slotRect = new Rect (x * 60, y * 60, 60, 60);
    61.                 slots [i] = inventory [i];
    62.                 if (slots [i].itemName != null) {
    63.                     GUI.DrawTexture (slotRect, slots [i].icon);
    64.                     if (slotRect.Contains (Event.current.mousePosition)) {
    65.                         toolTip = createToolTip (slots [i]);
    66.                         showToolTip = true;
    67.                         if (e.button == 0 && e.type == EventType.MouseDrag && !dragItem) {
    68.                             dragItem = true;
    69.                             draggedItem = slots [i];
    70.                         }
    71.                     }
    72.  
    73.  
    74.                     if (e.type == EventType.mouseUp && dragItem) {
    75.                             dragItem = false;
    76.                             draggedItem = null;
    77.                             for (int g = 0; g <items.Length; g++) {  
    78.                             ItemPlacement.SetItem (items [g]);
    79.                         }
    80.                     }
    81.  
    82.                     if (toolTip == "") {
    83.                         showToolTip = false;
    84.                     }
    85.                     i++;
    86.                 }
    87.             }
    88.         }
    89.     }
    90.    
    91.     string createToolTip (item Item){
    92.         toolTip = Item.itemName +  "\n" + Item.itemDisc;
    93.         return toolTip;
    94.     }
    95.  
    96.     void removeItem(int id){
    97.         for (int i = 0; i < inventory.Count; i++){
    98.             if (inventory [i].itemID == id){
    99.                 inventory[i] = new item();
    100.                     break;
    101.             }
    102.         }
    103.     }
    104.     void addItem(int id){
    105.         for (int i = 0; i < inventory.Count; i++) {
    106.             if (inventory [i].itemName == null) {
    107.                 inventory [i] = database.items[id];
    108.                 break;
    109.             }
    110.         }
    111.     }
    112.  
    113. }
    114.