- Home /
Switching sprite sheets at runtime using Unityscript
Near the start of my game the user selects one of two playable characters.
For either character, all the animations are the same. It's just the look of the character that's different.
I am trying to figure out a unityscript function that will load the appropriate sprite sheet and 're-skin' the character.
In searches, I found another thread that points to a helpful YouTube tutorial (starts at 20:00) 2D issues and how to solve them with Veli Pekka Kokkonen;
However, it's in C# and I'm not very good at converting some of the more complex C# script.
I need to convert it because the other option is to try to pass a variable from javascript to C# and I'm having difficulty doing that as well.
Everything I've figured out so far in javascript functions as it should, but I've hit a wall trying to work with the loop and array.
Can someone help me convert the rest of the C# code?
The variable spriteSheetName is either "Girl1" or "Girl2" and refer to the actual sprite sheet files which reside in 'Assets/Resources/kid/...'
The code is attached to the animated sprite.
Here's the C# script:
using UnityEngine;
using System;
public class ReSkinGemma : MonoBehaviour {
public string spriteSheetName = "Girl1";
void LateUpdate ()
{
var subSprites = Resources.LoadAll<Sprite>("kid/" + spriteSheetName);
foreach (var renderer in GetComponentsInChildren<SpriteRenderer>())
{
Debug.Log("renderer= " + renderer + "\n");
string spriteName = renderer.sprite.name;
var newSprite = Array.Find(subSprites, item => item.name == spriteName);
if (newSprite)
renderer.sprite = newSprite;
}
}
}
And, here's as far as I've gotten so far trying to convert it to unityscript:
#pragma strict
public var spriteSheetName: String = "Girl1";
public var subSprites: Object[];
function LateUpdate ()
{
subSprites = Resources.LoadAll("kid/Girl1", typeof(Sprite));
var renderer = GetComponentInChildren(SpriteRenderer);
Debug.Log("renderer= " + renderer + "\n");
// for (???)
// {
var spriteName : String = renderer.sprite.name;
// var newSprite = Array.Find(subSprites, item => item.name == spriteName);
// if (newSprite)
// renderer.sprite = newSprite;
// }
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613192958im_/https://answers.unity.com/themes/thub/images/avi.jpg)