- Home /
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);
}
}
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.
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!
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#.
Your answer
Follow this Question
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