Sprites being skipped over in sprite change script
I didn't get any answers to the first time I posted and I still can't get it running. I'm having issues where my multi-tier destruction script (a simple sprite change) is affecting other instances of the prefab. I'll destroy one building for all 3 tiers of the sprite change, quickly move over to an untouched building, and it'll skip all the other sprites, right to the tier 3 destroyed sprite. I've tried playing with coroutines, various loop iterations.. I just can't get it working and it's starting to really piss me off because I don't see a single reason why it would be skipping sprites like that. I need them to stay as prefabs because the final game will be procedurally generated. Can someone please at least explain to me why it's skipping them?
void BlowUp()
{
if (spriteRenderer.sprite == mainSprite)
{
if (Input.GetKeyDown(KeyCode.Space))
{
spriteRenderer.sprite = dmgSprite;
Debug.Log("Check 1");
return;
}
}
else if (spriteRenderer.sprite == dmgSprite)
{
if (Input.GetKeyDown(KeyCode.Space))
{
spriteRenderer.sprite = dmgSprite2;
Debug.Log("Check 2");
return;
}
}
else if (spriteRenderer.sprite == dmgSprite2)
{
if (Input.GetKeyDown(KeyCode.Space))
{
spriteRenderer.sprite = dmgSprite3;
Debug.Log("Check 3");
return;
}
}
}
Answer by UnityCoach · Oct 01, 2018 at 07:25 AM
If the script is on the same game object as the Sprite Renderer, and that you get that Renderer on Awake, like :
SpriteRenderer spriteRenderer;
void Awake ()
{
spriteRenderer = GetComponent<SpriteRenderer>();
}
Then, you shouldn't have any problem.
Although, there maybe a problem with your input logic. Where are you calling BlowUp
from? If you call it from Update()
, it'll be called on all instances. If you want to call it only on one of them, you need to have some sort of selection.
Here, I'm using OnMouseEnter and OnMouseExit, it requires a Collider2D on the Sprite, to filter the method call when the mouse is hovering the object.
using UnityEngine;
[RequireComponent (typeof (SpringRenderer))]
public class SpriteSwitch : MonoBehaviour
{
[SerializeField] Sprite[] dmgSprites;
int _index;
Sprite mainSprite;
SpriteRenderer spriteRenderer;
bool _hovering;
void Awake ()
{
spriteRenderer = GetComponent<SpriteRenderer>();
mainSprite = spriteRenderer.sprite;
}
void Update ()
{
if (Input.GetKeyUp(KeyCode.Space))
BlowUp();
}
void OnMouseEnter ()
{
_hovering = true;
}
void OnMouseExit ()
{
_hovering = false;
}
void BlowUp()
{
if (!_hovering)
return;
if (spriteRenderer.sprite == mainSprite)
{
spriteRenderer.sprite = dmgSprites[0];
_index = 0;
}
else
{
_index++;
if (_index >= dmgSprites.Length-1)
spriteRenderer.sprite = mainSprite;
else
spriteRenderer.sprite = dmgSprites[_index];
}
}
}
I've also updated the logic so that you have a variable number of dmgsprites.
Your answer
Follow this Question
Related Questions
Getting name of Spritesheet on runtime with C#? (Not individual SpriteName) 1 Answer
Problem Changing Sprites with an Array of Sprites 1 Answer
Need help trying to get button to light up sprite 1 Answer
Huge issue of inconsistent sprite rendering across different Mac / Win PCs (same build) - help? 1 Answer
Shader Graph - Inherit from Sprite Renderer or Image Component 0 Answers