using UnityEngine;
[System.Serializable]
public class InventoryItem{
public string Name;
public int Amount,Price;
}
public class CustomClass : MonoBehaviour {
public InventoryItem start;
public InventoryItem custom;
void Start () {
start = new InventoryItem();
start.Name = "Rock";
start.Amount = 1;
start.Price = 0;
custom = start;
}
void Update () {
custom.Amount++;
}
}
How you can observe the amount of the start variable also changes
when i edit the custom class;
how can i fix this? like Class.Initialize()?
ps: I cant edit the class to struct, cuz i have no access to the class, above its only a example
You can respond to a change in value by using property accessors, but if you don’t have access to the source code for the class, then you can’t change it. (Maybe you could wrap it, though.)
public class InventoryItem
{
private int _amount;
public int Amount
{
get
{
return _amount;
}
set
{
// write code here to do something when Amount is changed
_amount = value;
}
}
}