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;
}
}