Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Class Instatantiate

Discussion in 'Scripting' started by DarkX, Jan 31, 2017.

  1. DarkX

    DarkX

    Joined:
    Apr 6, 2014
    Posts:
    42
    Hello Guys;

    How can i initialize a class;
    Script
    like that
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. [System.Serializable]
    4. public class InventoryItem{
    5.     public string Name;
    6.     public int Amount,Price;
    7. }
    8. public class CustomClass : MonoBehaviour {
    9.     public InventoryItem start;
    10.     public InventoryItem custom;
    11.     void Start () {
    12.         start = new InventoryItem();
    13.         start.Name = "Rock";
    14.         start.Amount = 1;
    15.         start.Price = 0;
    16.         custom = start;
    17.     }
    18.     void Update () {
    19.         custom.Amount++;
    20.     }
    21. }
    22.  
    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
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    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.
     
    DarkX likes this.
  3. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    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.)

    Code (csharp):
    1. public class InventoryItem
    2. {
    3.  
    4.     private int _amount;
    5.     public int Amount
    6.     {
    7.         get
    8.         {
    9.             return _amount;
    10.         }
    11.  
    12.         set
    13.         {
    14.             // write code here to do something when Amount is changed
    15.             _amount = value;
    16.         }
    17.     }
    18.  
    19. }
     
    DarkX likes this.
  4. DarkX

    DarkX

    Joined:
    Apr 6, 2014
    Posts:
    42
    Thks,
    The reference object is rendertexture;;

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. namespace UnityStandardAssets.ImageEffects
    5. {
    6.     [ExecuteInEditMode]
    7.     [AddComponentMenu("Image Effects/GrabScreen")]
    8.     public class GrabScreen : MonoBehaviour
    9.     {
    10.         public RenderTexture Grab;
    11.         public CustomBlur Result;
    12.         public Material mat;
    13.         void OnRenderImage (RenderTexture source, RenderTexture destination) {
    14.             Grab = source;
    15.             Grab = Instantiate(Grab) as RenderTexture;
    16.             mat.mainTexture = Grab;
    17.             Result.Original = Grab;
    18.             Graphics.Blit(source,destination);
    19.         }
    20.     }
    21. }
    22.  
    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
     
  5. DarkX

    DarkX

    Joined:
    Apr 6, 2014
    Posts:
    42
    I Have no access to the source code, i cant edit it:

    :(