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
public void CreateGameObject(int x, int y)
{
// Generating new GameObject from Script
GameObject currentTileGO = new GameObject("Tile " x + " " + y);
// Add SpriteRenderer
SpriteRenderer spriteRenderer = currentTileGO.AddComponent<SpriteRenderer>();
// Add CustomScript (AnimatedTile)
AnimatedTile animScript = currentTileGO.AddComponent<AnimatedTile>();
// Get Sprite[] to initialize AnimatdTileScript
Sprite[] animationSprites = GetSprites(x, y);
if(animationSprites == null)
{
// Debug Message
Debug.LogError("NULL == animationSprites == GetSprites(x, y);");
return;
}
else
{
// Add Sprite[] to AnimatedTileScript
animScript.SetAnimation(animationSprites);
}
}
V1: does not work ( no Error Message but also no SpriteArray set)
public class AnimatedTile : MonoBehaviour {
SpriteRenderer spriteRenderer;
Sprite[] animationSprites;
public void SetAnimation(Sprite[] sprites)
{
if(sprites == null)
{
Debug.LogError("sprites == null");
return;
}
animationSprites = sprites;
spriteRenderer = this.GetComponent<SpriteRenderer>();
spriteRenderer.sprite = animationSprites[0];
}
}
V2: works (only change is → public Sprite[ ] animationSprites
public class AnimatedTile : MonoBehaviour {
SpriteRenderer spriteRenderer;
public Sprite[] animationSprites;
public void SetAnimation(Sprite[] sprites)
{
if(sprites == null)
{
Debug.LogError("sprites == null");
return;
}
animationSprites = sprites;
spriteRenderer = this.GetComponent<SpriteRenderer>();
spriteRenderer.sprite = animationSprites[0];
}
}
V3: works also [System.Serializable] MonoBehaviour + [SerialiezedField] Property
[System.Serializable]
public class AnimatedTile : MonoBehaviour {
SpriteRenderer spriteRenderer;
[SerialiezedField]
Sprite[] animationSprites;
public void SetAnimation(Sprite[] sprites)
{
if(sprites == null)
{
Debug.LogError("sprites == null");
return;
}
animationSprites = sprites;
spriteRenderer = this.GetComponent<SpriteRenderer>();
spriteRenderer.sprite = animationSprites[0];
}
}
with which version should i go?