- Home /
how do i make a simple inventory like in black ops?
I am making a simple inventory that has a max of like 3 things and can change out items for other ones. I need the item you pick up to change with the item you are holding.
heres what i have so far
 using UnityEngine;
 using System.Collections;
 
 public class MoneyManager : MonoBehaviour 
 {
     public int money = 500;
 
     public GameObject AR15;
 
     void OnGUI()
     {
         GUI.Label (new Rect (0,0,80,20), money.ToString(), "box");
     }
 
     void Update()
     {
         Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
         RaycastHit hit;
 
         if(Physics.Raycast(ray, out hit, 3))
         {
             //print("Hit "+hit.collider.name);
         
             if (hit.collider.tag == "Jug")
             {
                 print("Hit "+hit.collider.name);
                 if(Input.GetKeyDown(KeyCode.E) && money >= 2000)
                 {
                     money -= 2000;
                     hit.collider.gameObject.SendMessage("spawn", SendMessageOptions.DontRequireReceiver);
                 }
             }
                  //picking up a gun here
             if (hit.collider.tag == "ar15")
             {
                 print("Hit "+hit.collider.name);
                 if(Input.GetKeyDown(KeyCode.E) && money >= 1500)
                 {
                     money -= 1500;
                 }
             }
             
         }
     }
 }
i sorta got stuck on the inventory thing cant think of how to do it
Answer by Spinnernicholas · Dec 04, 2013 at 06:53 AM
A very simple way is to have an array of items and then track which one is active.
 public GameObject[] items = new GameObject[3];
 public int activeItem = 0;
 
 void switchItem(GameObject newItem)
 {
   detachActiveItem();
   attachActiveItem(newItem);
 }
 
 void detachActiveItem()
 {
   //Detach Active Item.
   items[activeItem] = null;
 }
 
 void attachActiveItem(GameObject newItem)
 {
   items[activeItem] = newItem;
   //Attach New Item
 }
how would i set this up cus i haven't relly used arrays that much yet
can you explain how this script works by putting more comments in the script so it makes more sence cus i dont rely get it that much. also can you have it change weapons with what a raycast hits.
This script is a bare bones skeleton, you will have to flesh it out and integrate it into your existing codebase.
Just a note, this code uses GameObjects to reference the items, you may want to use a custom monobehavior component ins$$anonymous$$d.
 //Declare and array of gameobjects and initialize the length to 3.
 public GameObject[] items = new GameObject[3];
 //Declare and initialize active item index. 0 = first item,....
 public int activeItem = 0;
 
 //This function will replace the active item with the newItem
 void switchItem(GameObject newItem)
 {
   detachActiveItem();
   attachActiveItem(newItem);
 }
 
 //Detaches the active item based on value of activeItem
 //This leaves the active item slot empty.
 void detachActiveItem()
 {
   //Todo: Detach Active Item.
   //Put code here to actually detach your item from the character.
   //Array Bookkeeping: if a position in the array is null,
   // then nothing is equipped in that slot.
   //Remove item from array
   items[activeItem] = null;
 }
 
 //Attaches newItem to the active item slot based on activeItem
 void attachActiveItem(GameObject newItem)
 {
   //Array Bookking: store newItem in array
   items[activeItem] = newItem;
   //Todo: Attach New Item
   //Add code here to actually attach the item to the character.
 }
I hope this helps some. But, like I said, this code is bare bones.
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Accessing scripts in the same game object 3 Answers
Explode Mesh when clicked on 1 Answer
Is there eval() for C#? 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                