up
still dont understand how i can drive this fields from script (
You would get a reference to the SpriteRenderer, generally with GetComponent() if the component is on the same object. Then use your reference to access the size variable.
using UnityEngine;
public class Example : MonoBehaviour {
private SpriteRenderer spriteRendererComponent;
private void Awake() {
spriteRendererComponent = GetComponent<SpriteRenderer>();
}
private void Start() {
if(spriteRendererComponent != null) {
spriteRendererComponent.size = Vector2.one; // any vector2
}
}
}
Thx for help, its works, i try before change size.y, and unity return me error - that variable only for read;
You have to set the whole Vector2, not just the X or just the Y.
So you can do this:
using UnityEngine;
public class Example : MonoBehaviour {
private SpriteRenderer spriteRendererComponent;
private void Awake() {
spriteRendererComponent = GetComponent<SpriteRenderer>();
}
private void Start() {
if(spriteRendererComponent != null) {
Vector2 newVector = spriteRendererComponent.size;
newVector.y = 5f; // any float
spriteRendererComponent.size = newVector;
// or
spriteRendererComponent.size.Set(spriteRendererComponent.size.x, 5f);
}
}
}
i solve this same way ) i understand about assign both x and y, mby u know how change pivot postion on sprite?
I dont understand how use this - https://docs.unity3d.com/ScriptReference/SpriteAlignment.html
SpriteAlignment is set on import in the SpriteEditor. I don’t think you can edit that at runtime.
=( bad new, im still need way to change pivot coordinates from sript. uGUI have tools for that, hope sprite have too
For a sprite, you can give it a parent gameobject, and then position the sprite using Local Position. Then use the parent for transformations to achieve the same pivot-like behavior.