Question by
GalaxyAzazel · Jul 14, 2016 at 02:08 PM ·
changespriterendereroverlap
Sprite change on click
I have a list of character sprites. I want them changed by clicking the mouse. The problem is that they change but еру previous sprite want disappear and they overlap on top of other. There are 2 scripts that I wrote
using UnityEngine;
public class change : MonoBehaviour {
public static int counter = 0;
public string resourceName = "Characters";
public Sprite[] characters;
SpriteRenderer currentSprite;
void Awake()
{
if (resourceName != "")
{
characters = Resources.LoadAll<Sprite>(resourceName);
}
}
// Use this for initialization
void Start () {
currentSprite = GameObject.Find("Character").GetComponent<SpriteRenderer>();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0) && counter < characters.Length)
{
if (currentSprite.sprite == null)
{
currentSprite.sprite = characters[counter];
currentSprite.enabled = true;
counter++;
}
else
{
currentSprite.sprite = null;
currentSprite.enabled = false;
}
}
}
}
this is the result:
and the second
using UnityEngine;
using System.Collections;
public class destroy : MonoBehaviour {
static int counter = 0;
string resourceName = "Characters";
Sprite[] backgrounds;
GameObject character;
SpriteRenderer currentSprite;
void Awake()
{
if (resourceName != "")
{
backgrounds = Resources.LoadAll<Sprite>(resourceName);
}
}
// Use this for initialization
void Start()
{
character = GameObject.Find("Character");
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) && counter < backgrounds.Length)
{
if (!character.GetComponent<SpriteRenderer>())
{
character.AddComponent<SpriteRenderer>();
currentSprite = character.GetComponent<SpriteRenderer>();
currentSprite.sprite = backgrounds[counter];
counter++;
}
else
Destroy(character.GetComponent<SpriteRenderer>());
}
}
}
Help please.
destroy.png
(136.9 kB)
change-4.png
(216.9 kB)
Comment