- Home /
Get first gameobject in a list and cycle through on keypress
Hi. I am trying to make an inventory in which the player can cycle through the inventory with a keypress. Right now, the current item that has been picked up by the player would be shown on the text. However, I want the player to be able to cycle through the previous items that he have picked up.
public KeyCode PickupButton;
public Text Players_Inventory;
public List<GameObject> Items = new List<GameObject>();
void Update ()
{
Input.GetKeyDown(PickupButton);
}
void OnTriggerStay(Collider other)
{
Debug.Log("Collision");
if (other.gameObject.tag == "Player" && Input.GetKeyDown(PickupButton))
{
Debug.Log("Button is pressed");
Items.Add(gameObject);
Destroy(gameObject);
Debug.Log("GameObject is destroyed");
Players_Inventory.text = "Items: " + (gameObject);
}
}
How do I cycle through a list of gameobject?
On a side note, when you inspect the Items list, are all the elements null/empty?
The object is added to the list but it is showing missing gameobject. So while the element of the list is increasing, it is showing as empty gameobject. Currently. I am using gameobject.setActive(false) as a alternative so I am still able to access the list.
What is this script attached to? You add gameObject
to Items
, but gameObject
refers to the game object to which the script is attached. I think you meant to use other.gameObject
.
$$anonymous$$oreover, you immediately Destroy()
gameObject
, so of course you will get "missing game object" - it has just been destroyed.
If this is a script for an inventory, then you want to add a data object to the list of items based on what kind of object the player picks up. Then you can destroy the item, which you can get from other.gameObject
. But possibly you don't need to do that, it all depends on your setup.
Answer by MT369MT · Jun 03, 2018 at 07:36 PM
Hi, I don’t understand how your script works... is this script attached to your items to pick up? And why are you destroying your object BEFORE you do: Players_Inventory... If you destroy the object the following lines will never be executed.
Anyway if you want to cycle throught an object list you can do something like this:
public List<GameObject> Items = new List<GameObject>();
public GameObject CurrentItemObject;
public int CurrentItemInt;
void Update()
{
if (Input.GetKeyDown(“right”)
{
CurrentItemInt += 1;
}
if (Input.GetKeyDown(“left”)
{
CurrentItemInt -= 1;
}
if (CurrentItemInt >= Items.Count)
{
CurrentItemInt = 0;
}
if (CurrentItemInt < 0)
{
CurrentItemInt = Items.Count - 1;
}
CurrentItemObject = Items[CurrentItemInt];
}
Yes. The script is attached to my item. Originally, it is attached to the player but it is not very smart of me to do so. I managed to get it to work. But I can't seems to change the displayed text according to the current Item.
CurrentItemobject = Items[0];
CurrentItemobject = Items[CurrentItem];
This is my current script. In the inspector, I can see that I am able to swap between the items that I've added to the list previously. However, the text that is displayed remains the same.
Change your Text script so:
Players_Inventory.text = "Items: " + CurrentItemObject;
If it doesn’t work send the complete script that you have now so I understand better.