i’m trying to make a sprite to be changed on specific button, i’ve already tried this code:
#pragma strict
var velocity : int = 8;
var sprite : SpriteRenderer;
function Start ()
{
sprite = GetComponent(SpriteRenderer);
}
function Update ()
{
if(Input.GetKey(KeyCode.LeftArrow))
{
sprite.sprite = Resources.Load("mario_3", typeof(Sprite));
transform.Translate(Vector2.right * -1 * velocity * Time.deltaTime);
}
if(Input.GetKey(KeyCode.RightArrow))
{
sprite.sprite = Resources.Load("mario_3", typeof(Sprite));
transform.Translate(Vector2.right * velocity * Time.deltaTime);
}
}
here i’m just interested in the sprite, i’ve got a multiple sprite splited but when i press the button to see if it works, the sprite disappears, i made a debug log on it to see what is going on and what it prompts is that script is being null when it tries to change.
i’ve seen some examples through the internet and they did not help, i also looked through the script reference and it does not seem to be that helpful in this case.
Update
i tried this code:
var frame : Sprite[];
private var sprite : SpriteRenderer;
function Start ()
{
//sprites are in folder Sprites
frame = Resources.LoadAll<Sprite>("Sprites/mario");
sprite = GetComponent(SpriteRenderer);
}
unity gives me this error: Operator ‘<’ cannot be used with a left hand side of type ‘function(String, System.Type): UnityEngine.Object’ and a right hand side of type ‘System.Type’.