Hello!
I’m using 2 scripts and one of them it’s responsible to change the SpriteRenderer sprite.
The hierarchy in my game project is:

- The ItemAnimation is the one with the script ItemController, which is responsible to control almost everything related to Items.
- Item is the one that should have its Sprite being changed
- Smoke is another GameObject with a SpriteRenderer that have an smoke animation.
- ItemRandomizer is a GameObject with a script named ItemRandomizer responsible for randomizing the items and delivering all the information regardind the item selected to ItemController (and it works perfectly).
ItemAnimation Inspector:

Item Inspector:
Smoke Inspector:
ItemRandomizer inspector:
ItemRandomizer.cs:
using UnityEngine;
using System.Collections.Generic;
[System.Serializable]
public class Item
{
public string itemName;
public Sprite itemImage;
public int itemAttackPoints;
public int itemLifePoints;
public int itemProficiencyPoints;
public int itemExcelencePoints;
public Item(string name, Sprite image, int attackPoints, int lifePoints, int proficiencyPoints, int excelencePoints)
{
this.itemName = name;
this.itemImage = image;
this.itemAttackPoints = attackPoints;
this.itemLifePoints = lifePoints;
this.itemProficiencyPoints = proficiencyPoints;
this.itemExcelencePoints = excelencePoints;
}
}
public class ItemRandomizer : MonoBehaviour
{
public Sprite[] itemSprites;
private static List<Item> availableItems = new List<Item>();
private void Start()
{
// adiciona os itens disponíveis na lista
availableItems.Add(new Item("EntropyBoots", itemSprites[0], 10, 20, 0, 0));
availableItems.Add(new Item("NightmareMonolyth", itemSprites[1], 0, -40, 5, 2));
availableItems.Add(new Item("BoneOfTheDead", itemSprites[2], 50, -80, 0, 0));
availableItems.Add(new Item("HollowLight", itemSprites[3], -10, 50, 2, 0));
availableItems.Add(new Item("SoulessGem", itemSprites[4], 0, -100, 10, 5));
availableItems.Add(new Item("DemonHands", itemSprites[5], 20, 50, -1, 0));
availableItems.Add(new Item("VoidHelmet", itemSprites[6], 100, 0, 15, 10)); // Setar vida para 1
availableItems.Add(new Item("HeavensHelmet", itemSprites[7], 0, 0, 0, 0)); // Setar vida para valor atual x2 e ataque para metade do valor atual
availableItems.Add(new Item("OrtraxAmulet", itemSprites[8], 20, 50, 5, 3));
availableItems.Add(new Item("LifeAmulet", itemSprites[9], -15, 80, 0, 0));
availableItems.Add(new Item("OrtraxRing", itemSprites[10], 10, 10, 0, 0)); //Se tiver ortrax amulet, dobrar os efeitos do ortrax ring, caso contrário apenas adiciona 10 de ataque e 10 de vida
availableItems.Add(new Item("CursedScroll", itemSprites[11], -30, -50, 10, 5));
availableItems.Add(new Item("AncientSkull", itemSprites[12], 10, 50, -2, 0));
availableItems.Add(new Item("BloodthirstySword", itemSprites[13], 50, -50, 10, 0));
availableItems.Add(new Item("AngelsChestpiece", itemSprites[14], -10, 100, 0, 4));
}
public Item GetRandomItem()
{
// escolhe um item aleatório da lista de itens disponíveis
int index = Random.Range(0, availableItems.Count);
Item item = availableItems[index];
// remove o item escolhido da lista de itens disponíveis
availableItems.RemoveAt(index);
return item;
}
}
ItemController script:
using UnityEngine; 
public class ItemController : MonoBehaviour
{
public Animator animator;
public float amplitude = 0.1f;
public float speed = 4f;
private Item itemGameObject;
private GameObject itemObject;
private Vector3 originalPosition;
private SpriteRenderer spriteRenderer;
private void Start()
{
ItemRandomizer itemRandomizer = FindObjectOfType<ItemRandomizer>();
itemGameObject = itemRandomizer.GetRandomItem();
itemObject = GameObject.Find("Item");
spriteRenderer = itemObject.GetComponent<SpriteRenderer>();
Sprite itemSprite = itemGameObject.itemImage;
spriteRenderer.sprite = itemSprite;
originalPosition = itemObject.transform.position;
animator.Play("Appear");
Invoke("ActivateItem", animator.GetCurrentAnimatorStateInfo(0).length);
}
private void Update()
{
if (animator.GetCurrentAnimatorStateInfo(0).IsName("Bouncing"))
{
float yPosition = originalPosition.y + amplitude * Mathf.Sin(speed * Time.time);
itemObject.transform.position = new Vector3(originalPosition.x, yPosition, originalPosition.z);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("ItemCollider"))
{
// Exibe a mensagem de confirmação
Debug.Log("Deseja pegar o item? Pressione Enter para confirmar.");
// Aguarda a entrada do jogador
if (Input.GetKeyDown(KeyCode.Return))
{
// Adiciona o item ao inventário
Inventory inventory = collision.gameObject.GetComponent<Inventory>();
if (inventory.AddItem(itemGameObject))
{
// Se o item foi adicionado com sucesso, destrói o objeto de item
Destroy(gameObject);
}
}
}
}
private void ActivateItem()
{
animator.Play("Bouncing");
Debug.Log("Item " + itemGameObject.itemName + " ativado");
Debug.Log("Vida atual: " + PlayerCharacter.GetCurrentLifePoints());
Debug.Log("Ataque atual: " + PlayerCharacter.GetCurrentAttackPoints());
if (itemGameObject.itemName == "VoidHelmet")
{
PlayerCharacter.SetLifePoints(1);
PlayerCharacter.AddAttackPoints(PlayerCharacter.GetCurrentAttackPoints() + itemGameObject.itemAttackPoints);
PlayerCharacter.AddProficiencyPoints(PlayerCharacter.GetCurrentProficiencyPoints() + itemGameObject.itemProficiencyPoints);
PlayerCharacter.AddExcelencePoints(PlayerCharacter.GetCurrentExcelencePoints() + itemGameObject.itemExcelencePoints);
}
else if (itemGameObject.itemName == "HeavensHelmet")
{
PlayerCharacter.SetLifePoints(PlayerCharacter.GetCurrentLifePoints() * 2);
PlayerCharacter.SetAttackPoints(PlayerCharacter.GetCurrentAttackPoints() / 2);
}
// else if (itemGameObject.itemName == "OrtraxRing")
// {
// if (HasOrtraxAmulet())
// {
// PlayerCharacter.AddAttackPoints(PlayerCharacter.GetCurrentAttackPoints() + itemGameObject.itemAttackPoints*2);
// PlayerCharacter.AddLifePoints(PlayerCharacter.GetCurrentLifePoints() + itemGameObject.itemLifePoints*2);
// }
// else
// {
// PlayerCharacter.AddAttackPoints(PlayerCharacter.GetCurrentAttackPoints() + itemGameObject.itemAttackPoints);
// PlayerCharacter.AddLifePoints(PlayerCharacter.GetCurrentLifePoints() + itemGameObject.itemLifePoints);
// }
// }
else
{
PlayerCharacter.AddLifePoints(PlayerCharacter.GetCurrentLifePoints() + itemGameObject.itemLifePoints);
PlayerCharacter.AddAttackPoints(PlayerCharacter.GetCurrentAttackPoints() + itemGameObject.itemAttackPoints);
PlayerCharacter.AddProficiencyPoints(PlayerCharacter.GetCurrentProficiencyPoints() + itemGameObject.itemProficiencyPoints);
PlayerCharacter.AddExcelencePoints(PlayerCharacter.GetCurrentExcelencePoints() + itemGameObject.itemExcelencePoints);
}
Debug.Log("Vida atual: " + PlayerCharacter.GetCurrentLifePoints());
Debug.Log("Ataque atual: " + PlayerCharacter.GetCurrentAttackPoints());
}
}
I don’t know what could possibly be the problem. If I manually change the sprite of Item, everything works perfectly. The proble is that the sprite is not being changed by the script. I did debug everything possible, the image is correctly acessed and passed via scripts.


