Class Instatantiate

Hello Guys;

How can i initialize a class;
Script
like that

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

Thnks in advance

custom = start means these reference the same object. Changing the value of one changes the value of the other.

You’d have to have custom = new InventoryItem(); and then set it’s values to what start has.

1 Like

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

}
1 Like

Thks,
The reference object is rendertexture;;

using UnityEngine;

namespace UnityStandardAssets.ImageEffects
{
    [ExecuteInEditMode]
    [AddComponentMenu("Image Effects/GrabScreen")]
    public class GrabScreen : MonoBehaviour
    {
        public RenderTexture Grab;
        public CustomBlur Result;
        public Material mat;
        void OnRenderImage (RenderTexture source, RenderTexture destination) {
            Grab = source;
            Grab = Instantiate(Grab) as RenderTexture;
            mat.mainTexture = Grab;
            Result.Original = Grab;
            Graphics.Blit(source,destination);
        }
    }
}

when i set to another object, its will blur(blur screen Effect after that script) because grag is equal to the source;
and source will be edited;

how can i set a new reference to not be edited

I Have no access to the source code, i cant edit it:

:frowning: