- Home /
How to modify array values?
Hello! I've a little problem with arrays and lists. I'm working on a pretty complicated game as an exercise, in this game you can place and remove blocks like Minecraft, so you have an inventory (5 slots) where you can deposit your blocks. When I pick a block from the ground, the array ofthe inventory has to be modified, but when simply modifying the value of the slot Items[0] = ItemID;
it doesn't change at all.
This is my main script: ( feel free to correct it! )
![using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
using UnityEngine.UI;
using LogService;
public class Player : MonoBehaviour
{
[Header("Configurations")]
public float Speed;
public int Health;
[Header("Tile Data Table")]
public List<string> Name;
public List<Sprite> Icon;
public List<int> Clip;
public List<bool> Collision;
public List<Object> ItemPrefab;
[Header("Tiling")]
public List<Tile> Tiles;
public List<AudioClip> PlacingSounds;
[Header("User Interface")]
public List<int> Items;
public GameObject[] Slots;
public Image[] ItemIcon;
public GameObject SelectionSprite;
public Text HealthText;
[Header("Assets")]
public Tilemap Tilemap;
public Tilemap TilemapC;
public AudioSource AudioSource;
private int health;
private int ID;
// Main
void Start()
{
Log LogSession = new Log();
LogSession.Setup();
LogSession.Print("Player Logged In");
health = Health;
LogSession.Print("Player Health = " + health + "/" + Health);
LogSession.Print("Player Speed = " + Speed);
foreach(Image I in ItemIcon) { I.enabled = false; }
}
private void Update()
{
Move();
Refresh();
Interact();
}
// User Actions
void Move()
{
Vector2 movement = new Vector2();
if (Input.GetKey(KeyCode.D)) { movement.x = +1; }
else if (Input.GetKey(KeyCode.A)) { movement.x = -1; }
else if (Input.GetKey(KeyCode.W)) { movement.y = +1; }
else if (Input.GetKey(KeyCode.S)) { movement.y = -1; }
else { movement = new Vector2(); }
GetComponent<Rigidbody2D>().velocity = movement * Speed;
}
void Interact()
{
Vector3Int Position = Tilemap.WorldToCell(Vector3Int.FloorToInt(Camera.main.ScreenToWorldPoint(Input.mousePosition)));
Vector3 WorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 VisibleWP = new Vector3(WorldPosition.x, WorldPosition.y, 0);
if (Input.GetKeyDown(KeyCode.Alpha1)) { ID = Items[0]; Select(Slots[0]); }
if (Input.GetKeyDown(KeyCode.Alpha2)) { ID = Items[1]; Select(Slots[1]); }
if (Input.GetKeyDown(KeyCode.Alpha3)) { ID = Items[2]; Select(Slots[2]); }
if (Input.GetKeyDown(KeyCode.Alpha4)) { ID = Items[3]; Select(Slots[3]); }
if (Input.GetKeyDown(KeyCode.Alpha5)) { ID = Items[4]; Select(Slots[4]); }
if (Input.GetMouseButtonDown(0))
{
if (Collision[ID] == true)
{
if (TilemapC.HasTile(Position) == false) { AudioSource.PlayOneShot(PlacingSounds[Clip[ID]]); TilemapC.SetTile(Position, Tiles[ID]); }
}
else
{
Tilemap.SetTile(Position, Tiles[ID]);
}
}
if (Input.GetMouseButtonDown(1) && TilemapC.GetTile(Position) != null)
{
if (TilemapC.GetTile(Position).name == "Stone")
{
Instantiate(ItemPrefab[1], VisibleWP, Quaternion.Euler(0, 0, 0)); TilemapC.SetTile(Position, null);
}
else if (TilemapC.GetTile(Position).name == "Wood")
{
Instantiate(ItemPrefab[2], VisibleWP, Quaternion.Euler(0, 0, 0)); TilemapC.SetTile(Position, null);
}
else if (TilemapC.GetTile(Position).name == "Bricks")
{
Instantiate(ItemPrefab[3], VisibleWP, Quaternion.Euler(0, 0, 0)); TilemapC.SetTile(Position, null);
}
}
if (health <= 0 || health == 0)
{
Destroy(gameObject);
}
}
// User Interactions
public void Damage(int amount) { health -= amount; }
public void Heal(int amount) { health += amount; }
// User Interface
void Select(GameObject Slot)
{
SelectionSprite.GetComponent<Transform>().position = Slot.transform.position;
}
void Refresh()
{
float p = Health / 100 * health;
HealthText.text = p + "%";
}
public void Give(int ItemIdentifier)
{
Items[0] = ItemIdentifier;
}
}
[1]: /storage/temp/172998-immagine-2020-12-23-170408.png
Answer by Llama_w_2Ls · Dec 23, 2020 at 06:45 PM
This line: Items[0] = ItemID;
Is definitely valid, and can be used to set the value of an item in an array or list. However, nowhere in the code have you defined the values of the list Items
. In the editor, you set the size, but not the actual elements. If you give the list some elements, is the inventory modified? @DaRealPoopMaker
"Items" is the array that defines the identifier numbers of the objects that I have in my slot: for example, if i have a stone block in my first slot, the Items[0] value will change to the stone block ID. I've never defined my array because I just don't have to, I want the array to automatically change whenever the Item I pick from the ground (which has another script I've made) calls the method Give ().
The answer to your question is yes, but actually no: the inventory gets modified only when I manually set the object Id via Inspector tab, but not when I call the Give () method via Script (which is is the issue I have.)
Thanks for reading and replying, by the way!
Where exactly do you call the Give() method. All seems to be working fine, but possibly the way you call it seems to be the issue. @Globglobgabgalab
Here's the script where I call it:
using UnityEngine;
public class Item : $$anonymous$$onoBehaviour
{
[Header("Configurations")]
public int ItemID;
public float RotationSpeed;
[Header("Assets")]
public Player P;
private void Update()
{
Rigidbody2D RB = GetComponent<Rigidbody2D>();
Collider2D C = GetComponent<Collider2D>();
Vector3 $$anonymous$$ousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RB.rotation += RotationSpeed/10;
if (Input.GetKeyDown(KeyCode.E) && C.OverlapPoint($$anonymous$$ousePosition))
{
P.Give(ItemID);
Destroy(gameObject);
}
}
}