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 Entyro · Apr 11, 2014 at 01:32 PM · c#inventory

Inventory system Help

Hi

I have an "inventory" and "pick up" script almost done. The thing I have a problems with is to use the items I'm picking up. I'm using it for mostly a battery inventory so when I've picked up a bettery I use it with the help of this little code "Flashlight.AlterEnergy(batteryPower);". And that works perfectly.

But I want the game to check if I'm having something called "Batteries" in my inventory.

If so --> Use it, if not --> Don't use it. Simple as that.

Inventory script:

 using UnityEngine;
 using System.Collections;
 
 public class Inventory : MonoBehaviour {
     //GUI Skin
     public GUISkin InvSkin;
     //Slots
     public static string slot1 = "Empty 1";
     public static string slot2 = "Empty 2";
     public static string slot3 = "Empty 3";
     public static string currentItem = "None";
     //Other Vars
     private bool isenabled = false;
     private int messageint = 0;
     private string messagetext = "";
     private bool remove = false;
     private int batteryPower = 100;
     private Flashlight jsScript;
 
     void Update()
     {
             jsScript = this.GetComponent<Flashlight>();
        
             if (messageint >= 0)
             {
                 messageint--;
             }
 
         if (Input.GetKeyDown(KeyCode.Alpha1))
         {
             currentItem = slot1;
             messageint = 100;
             messagetext = slot1 + " Used";
             ActivateItems(currentItem);
         }
         else if (Input.GetKeyDown(KeyCode.Alpha2))
         {
             currentItem = slot2;
             messageint = 100;
             messagetext = slot2 + " Used";
             ActivateItems(currentItem);
         }
         else if (Input.GetKeyDown(KeyCode.Alpha3))
         {
             currentItem = slot3;
             messageint = 100;
             messagetext = slot3 + " Used";
             ActivateItems(currentItem);
         }
         if (Input.GetKeyDown(KeyCode.Tab))
         {
             if (isenabled)
             {
                 isenabled = false;
             }
             else if (!isenabled)
             {
                 isenabled = true;
             }
         }
     }
 
     void OnGUI()
     {
         GUI.skin = InvSkin;
         if (messageint > 1)
         {
             GUI.Label(new Rect(Screen.width/2-50, Screen.height/2-55, 120, 50),messagetext);
         }
         if (isenabled)
         {
             if (GUI.Button(new Rect(Screen.width / 2 - 170, Screen.height / 2 + 390, 100, 30), slot1))
             {
                 if (!remove)
                 {
                     currentItem = slot1;
                     messageint = 100;
                     messagetext = slot1 + " Used";
                     ActivateItems(currentItem);
                     slot1 = "Empty 1";
                 }
                 else
                 {
                     messageint = 100;
                     messagetext = slot1 + " Removed";
                     slot1 = "Empty 1";
                     remove = false;
                 }
             }
             if (GUI.Button(new Rect(Screen.width / 2 - 40, Screen.height / 2 + 390, 100, 30), slot2))
             {
                 if (!remove)
                 {
                     currentItem = slot2;
                     messageint = 100;
                     messagetext = slot2 + " Used";
                     ActivateItems(currentItem);
                     slot2 = "Empty 2";
                 }
                 else
                 {
                     messageint = 100;
                     messagetext = slot2 + " Removed";
                     slot2 = "Empty 2";
                     remove = false;
                 }
             }
             if (GUI.Button(new Rect(Screen.width / 2 +90, Screen.height / 2 + 390, 100, 30), slot3))
             {
                 if (!remove)
                 {
                     currentItem = slot3;
                     messageint = 100;
                     messagetext = slot3 + " Used";
                     ActivateItems(currentItem);
                     slot3 = "Empty 3";
                 }
                 else
                 {
                     messageint = 100;
                     messagetext = slot3 + " Removed";
                     slot3 = "Empty 3";
                     remove = false;
                 }
             }
             if (remove)
             {
                 if (GUI.Button(new Rect(Screen.width / 2 + 300, Screen.height / 2 + 390, 100, 30), "Stop"))
                 {
                     remove = false;
                 }
             }
             else if (!remove)
             {
                 if (GUI.Button(new Rect(Screen.width / 2 + 300, Screen.height / 2 + 390, 100, 30), "Remove"))
                 {
                     remove = true;
                 }
             }
         }
     }
 
     void ActivateItems(string slot)
     {
         Flashlight.AlterEnergy(batteryPower);
     }
 }

Pick up script:

 using UnityEngine;
 using System.Collections;
 
 public class PickUpItem : MonoBehaviour 
 {
     public Transform Player;
     public GUISkin InvSkin;
     public string ItemName;
     public AudioClip PickUpSound;
     public AudioClip CantPickUpSound;
     public bool displaymessage;
     void Update () 
     {
         if (Vector3.Distance(Player.position, transform.position) < 3)
         {
             displaymessage = true;
             if (Input.GetKeyDown(KeyCode.E))
             {
                 if (Inventory.slot1 == "Empty 1")
                 {
                     Inventory.slot1 = ItemName;
                     AudioSource.PlayClipAtPoint(PickUpSound, Camera.main.transform.position);
                     DestroyGO();
                 }
                 else if (Inventory.slot2 == "Empty 2")
                 {
                     Inventory.slot2 = ItemName;
                     AudioSource.PlayClipAtPoint(PickUpSound, Camera.main.transform.position);
                     DestroyGO();
                 }
                 else if (Inventory.slot3 == "Empty 3")
                 {
                     Inventory.slot3 = ItemName;
                     AudioSource.PlayClipAtPoint(PickUpSound, Camera.main.transform.position);
                     DestroyGO();
                 }
                 else
                 {
                     AudioSource.PlayClipAtPoint(CantPickUpSound, Camera.main.transform.position);
                 }
             }
         }
         else
         {
             displaymessage = false;
         }
     }
 
     void OnGUI()
     {
         GUI.skin = InvSkin;
         if (displaymessage)
         {
             GUI.Label(new Rect(Screen.width/2-50, Screen.height/2-55, 120, 50),"[E] Pick up " + ItemName);
         }
     }
 
     void DestroyGO()
     {
         GameObject.Destroy(gameObject);
     }
 }
Comment
Add comment
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

2 Replies

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

Answer by nicolasjr · Apr 11, 2014 at 01:42 PM

Hi Rise. If you allow me to offer you some tips that may be useful for you in this implementation, let me do so.

1- I think that the best way to implement a inventory would be by creating a list of Items. It simply makes more sense in the object oriented perspective.

2- Instead of keeping track of items as strings, they should be a class, with attributes. Based on your code, I believe that your items gives a specific charge to the flashlight, isn't it correct? So it should be a variable of the Item class.

3- You're setting the variable jsScript inside a Update function, which means your setting it over and over, when it doesn't seem to be changing.

4- Now I'll start answering your question: If you create the list of Items and the Item class, you can have an enumeration inside the class that can let your system knows the type of Item, and, with that, you can simply verify if you have a Item Battery inside your Items list, that's being held by the inventory.

Hope I could make sense to you. Let me know if you have any further question.

Comment
Add comment · Show 1 · 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 Entyro · Apr 11, 2014 at 01:50 PM 0
Share

I'm actually not a c# guy, I'm working with JavaScript mostly, I found this script on a forum and I've done som small adjustments. So I didn't get all you said, but I think I can figure it out, with the help of Google of course :)

Thanks for the fast answer, I will test it out and come back to you if I'm having trouble!

avatar image
0

Answer by GoGamingJoe · Apr 11, 2014 at 04:00 PM

One way to do it is to have another script to say when you pick up the battery add 1 to it then the flashlight woud check that if it was more than 0 and then it would use it. Then use nico idea of a List so you could use the remove command in C#.

Comment
Add comment · 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

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

C# scriping help 0 Answers

Tutorial level 2 Answers

GetComponent().enabled = true; 1 Answer

c# how to get to idle from walk 0 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