Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by iamthecoolguy11 · Dec 04, 2013 at 06:34 AM · c#javascriptinventory

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iamthecoolguy11 · Dec 04, 2013 at 06:49 AM 0
Share

:) :) :) :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iamthecoolguy11 · Dec 04, 2013 at 06:55 AM 0
Share

how would i set this up cus i haven't relly used arrays that much yet

avatar image iamthecoolguy11 · Dec 04, 2013 at 06:59 AM 0
Share

ok i get it thanks

avatar image Spinnernicholas · Dec 04, 2013 at 04:26 PM 0
Share

If you have anymore questions, let me know.

avatar image iamthecoolguy11 · Dec 04, 2013 at 10:56 PM 0
Share

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.

avatar image Spinnernicholas · Dec 04, 2013 at 11:42 PM 1
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges