Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by danizufrique · Dec 21, 2019 at 08:31 PM · arraysreferencinggrid based gamecustom classclass instance

Error referencing custom class elements

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;
   }
 }
  
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Arrays and Classes Javascript 1 Answer

creating and manipulating a grid efficiently 1 Answer

3D Grid Based Movement (X-Com 2012 etc) – How to implement vertical movement? 0 Answers

How do i reference a path as a sprite? 1 Answer

Search for the index in an array of a customised class 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges