0
\$\begingroup\$

I wanted to create a custom array that contains both strings and integers to represent the name and amount of an item a player has. While this did work in the inspector, I don't know how to access or change values or add new elements in a custom array during gameplay. The website that I used as a guide had 2 functions, but they unfortunately didn't work. How can I edit a custom array during gameplay?

Here is the minimum required code:

using System;
using System.Collections.Generic;
using UnityEngine;
public class CustomArrayTest: MonoBehaviour 
{
    [SerializeField] private PlayerScrolls[] playerScrolls;
    public void SetValue () 
    {
        //Function to set a value in the array
    }
    public SubClass GetValue () 
    {
        //Function to get a value in the array
    }
}
[System.Serializable]
public class PlayerScrolls 
{
    public int scrollAmount;
    public string scrollName;
}

Website I based his code off of: https://owlcation.com/stem/Using-Single-and-Multi-Dimensional-Arrays-in-C-and-Unity3D

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Getting your code to work is the easier part. You would need to change your getter and setter to match what the tutorial is teaching you. A setter needs the parameter to well, set things. Your getter returns a totally different class than you would expect, it should be of type PlayerScrolls.

public void SetValue (PlayerScrolls item, int index) {
    playerScrolls[index] = item;
}

public PlayerScrolls GetValue (int index) {
    return playerScrolls[index];
}

But this gives you a new challenge. How do you know the index? While for some kind of games it might work (like you have 4 inventory slots and the player presses the first slot, so you would know index = 0), even adding an item will be a problem. You would need first to find out, where did you store it. Afterwards retrieve it, adjust the item amount and store it back. Your array has no knowledge where it actually stores things. And you might run into index problems if you try to access parts that are not allocated.

Unity offers a tutorial which might interest you about Lists and Dictionaries which might be a good start to check out.

Edit to your comment:

If you would like to add an amount to an already existing item, you could do it like this. I'm reusing the previous getter/ setter.

public void AddItemAmount(int amount, int index) {
   PlayerScrolls item = GetValue(index);
   item.scrollAmount += amount;
   SetValue(item, index);
}
\$\endgroup\$
2
  • \$\begingroup\$ So the way the setter works is that you input the parameters for how much of an item you want to enter and its index (e.g. to add 3 scrolls to index 2 would be to type SetValue(3,2) )? Luckily in my game players have a set number of inventory slots all of which are designated to hold one item, so I don't think indexing will be a problem. \$\endgroup\$ Dec 29, 2021 at 10:44
  • \$\begingroup\$ It would not work that way as it currently is. But you could change item to accept a value to add it. You would just need to make sure there is actually an item already at that slot \$\endgroup\$
    – Zibelas
    Dec 29, 2021 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .