Quantcast
Channel: Questions in topic: "class instance"
Viewing all articles
Browse latest Browse all 131

Error referencing custom class elements

$
0
0
Hello everyone. I've been working in a grid based project in which I save an instance of the class "Cell" in a "grid[ , ]" array, that represents each particular cell of the grid and conteins information about it, including a reference to the character in it. The problem is that, if I try to modify the stats of that character nothing happens in the game, but the methods of the class are called, as if I was accesing a copy of the character. I've tried fixing this passing the character and the cell by reference, but still not working, and as I have read, pointers in C# are not the best option, so, any idea on how I could fix this? Thanks. This is where I try to modify the stats: void ReplaceForWall(ref Cell cell){ cell.FreeCell(); bool combined = false; Vector2Int pos = cell.gridPositionNormalized; int playerMod = (xScale == 1) ? 1 : -1; // Vemos si en la siguiente casilla tiene otro muro if( (pos.x < grid.GetLength(0)-1 && xScale == 1) || (pos.x>0 && xScale == -1) ){ Character posibleWall = grid[pos.x+playerMod, pos.y].host; if(posibleWall != null){ if(posibleWall.alias == "Wall"){ posibleWall.maxHealth += posibleWall.maxHealth; posibleWall.Heal(posibleWall.maxHealth); combined = true; } } } if(!combined){ Character instance = Instantiate(Wall, cell.worldPosition, Quaternion.identity); InitInstance(ref instance, ref cell); } } ------- using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Character : MonoBehaviour { public string alias; public int maxHealth; public int currentHealth; public int waitTurn; public int damage; public int size; public int id; public Cell cell; public Text HUB_Health; void Start(){ currentHealth = maxHealth; HUB_Health.text = currentHealth.ToString(); } public virtual void Attack(){ Debug.Log(alias + "is attacking"); } public void Heal(int hp){ currentHealth += hp; HUB_Health.text = currentHealth.ToString(); Debug.Log("Healing"); //Show in console, but health does not increase in real game } } --------- using System.Collections; using System.Collections.Generic; using UnityEngine; public class Cell { public Vector2Int gridPosition; public Vector2Int gridPositionNormalized; public Vector3 worldPosition; public string name; public bool occupied; public Character host; public Cell(){ name = ""; gridPosition = Vector2Int.zero; worldPosition = Vector3.zero; occupied = false; host = null; } public Cell(Vector2Int tPos, Vector3 wPos){ name = ""; gridPosition = tPos; worldPosition = wPos; occupied = false; host = null; } public void FreeCell(){ UnityEngine.Object.Destroy(host.gameObject); occupied = false; host = null; } }

Viewing all articles
Browse latest Browse all 131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>