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

EditorScripting: change private Properties from CustomScript added to Editor-generated GameObjects

Discussion in 'Scripting' started by tobad, Apr 23, 2015.

  1. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    Hey Guys,

    I'm trying to create GameObjects dynamically in the Editor.
    Adding SpriteRenderer and a Custom Script works flawless. But when I change private properties in the CustomScript with a Method "public SetProperty(T value)" the changes are not applied unless i set the propertie to public or add the [SerialiezedField] markup.

    Is this behaviour normal, if yes... WHY?
    What I know is that Unity can only change/view property value in Inspector if property is public or marked with [SerializedField] but what is with the default (initializing value) how are they applied?

    Shoud I stick with
    v2:
    public property
    v3:
    [System.Serializable] MonoBehaviour + [SerialiezedField] Property
    or are there other ways ??

    Note: AnimatedTile this is a MonoBehaviour Script, not a ScriptableObject.


    Editor Script

    Code (CSharp):
    1. public void CreateGameObject(int x, int y)
    2. {
    3.     // Generating new GameObject from Script
    4.     GameObject currentTileGO = new GameObject("Tile " x + " " + y);
    5.  
    6.     // Add SpriteRenderer
    7.     SpriteRenderer spriteRenderer = currentTileGO.AddComponent<SpriteRenderer>();
    8.  
    9.     // Add CustomScript (AnimatedTile)
    10.     AnimatedTile animScript = currentTileGO.AddComponent<AnimatedTile>();
    11.  
    12.     // Get Sprite[] to initialize AnimatdTileScript
    13.     Sprite[] animationSprites = GetSprites(x, y);
    14.     if(animationSprites == null)
    15.     {
    16.         // Debug Message
    17.         Debug.LogError("NULL == animationSprites == GetSprites(x, y);");
    18.         return;
    19.     }
    20.     else
    21.     {
    22.         // Add Sprite[] to AnimatedTileScript
    23.         animScript.SetAnimation(animationSprites);
    24.     }
    25. }
    V1: does not work ( no Error Message but also no SpriteArray set)
    Code (CSharp):
    1. public class AnimatedTile : MonoBehaviour {
    2.     SpriteRenderer spriteRenderer;
    3.     Sprite[] animationSprites;
    4.  
    5.     public void SetAnimation(Sprite[] sprites)
    6.     {
    7.         if(sprites == null)
    8.         {
    9.             Debug.LogError("sprites == null");
    10.             return;
    11.         }
    12.         animationSprites = sprites;
    13.         spriteRenderer = this.GetComponent<SpriteRenderer>();
    14.         spriteRenderer.sprite = animationSprites[0];
    15.     }
    16. }
    V2: works (only change is -> public Sprite[] animationSprites
    Code (CSharp):
    1. public class AnimatedTile : MonoBehaviour {
    2.     SpriteRenderer spriteRenderer;
    3.     public Sprite[] animationSprites;
    4.  
    5.     public void SetAnimation(Sprite[] sprites)
    6.     {
    7.         if(sprites == null)
    8.         {
    9.             Debug.LogError("sprites == null");
    10.             return;
    11.         }
    12.         animationSprites = sprites;
    13.         spriteRenderer = this.GetComponent<SpriteRenderer>();
    14.         spriteRenderer.sprite = animationSprites[0];
    15.     }
    16.  
    17. }
    V3: works also [System.Serializable] MonoBehaviour + [SerialiezedField] Property
    Code (CSharp):
    1. [System.Serializable]
    2. public class AnimatedTile : MonoBehaviour {
    3.     SpriteRenderer spriteRenderer;
    4.     [SerialiezedField]
    5.     Sprite[] animationSprites;
    6.  
    7.     public void SetAnimation(Sprite[] sprites)
    8.     {
    9.         if(sprites == null)
    10.         {
    11.             Debug.LogError("sprites == null");
    12.             return;
    13.         }
    14.         animationSprites = sprites;
    15.         spriteRenderer = this.GetComponent<SpriteRenderer>();
    16.         spriteRenderer.sprite = animationSprites[0];
    17.     }
    18. }
    19.  
    with which version should i go?
     
    Last edited: Apr 23, 2015
  2. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    *bump*