this is the most confusing problem I’ve ever encountered. a script is attached to a object with the image component and is constantly setting the source image. before runtime I can change the source in the inspector. in runtime that same script and its parent’s script both say in their debug logs that image.sprite is set correctly to the sprites I want it to be. there are 0 oddities with the debug logs I had (so i removed the logs), and 0 errors. somehow in runtime not only is it not visually changing, but I cant use the inspector to change the object.
Image changing script
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class ItemWobbleAnimatior : MonoBehaviour
{
Animator animator;
Image image;
Dictionary<string, Sprite> spriteDictionary = new Dictionary<string, Sprite>();
void Awake()
{
animator = GetComponent<Animator>();
image = GetComponent<Image>();
// Load all sprites from the "Items" folder into a dictionary
Sprite[] sprites = Resources.LoadAll<Sprite>("Items");
foreach (Sprite sprite in sprites)
{
// Add the sprite to the dictionary with its name as the key
spriteDictionary[sprite.name] = sprite;
}
}
void Update()
{
string itemName = Inventory.item[0]; // Assuming Inventory.item is an array of strings
// Calculate the state progress of the animator
float stateProgress = animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
stateProgress %= 1f;
// Construct sprite name based on item name and state progress
string spriteName = itemName + (stateProgress <= 0.5f ? "0" : "1");
// Check if the sprite exists in the dictionary
if (spriteDictionary.ContainsKey(spriteName))
{
// Set the sprite of the Image component
image.sprite = spriteDictionary[spriteName];
}
else
{
Debug.LogError("Sprite not found with name: " + spriteName);
}
}
}
Parent script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Unity;
using UnityEngine.UI;
public class HandAnimations : MonoBehaviour
{
public static Animator animator;
public Sprite emptySprite;
private void Awake()
{
animator = GetComponent<Animator>();
}
private void Update()
{
Image childimage = transform.GetChild(0).GetComponent<Image>();
Animator childAnimator = transform.GetChild(0).GetComponent<Animator>();
animator.SetBool("Holding Item", childimage.sprite != null);
if (Inventory.item[0] == null)
{
childimage.sprite = emptySprite;
}
/*
if (Inventory.item[1] == null)
{
childimage.sprite = emptySprite;
}
*/
Debug.Log(childimage.sprite);
}
// OnStateEnter is called before OnStateEnter is called on any state inside this state machine
//override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
//
//}
// OnStateUpdate is called before OnStateUpdate is called on any state inside this state machine
//override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
//
//}
// OnStateExit is called before OnStateExit is called on any state inside this state machine
//override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
//
//}
// OnStateMove is called before OnStateMove is called on any state inside this state machine
//override public void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
//
//}
// OnStateIK is called before OnStateIK is called on any state inside this state machine
//override public void OnStateIK(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
//
//}
// OnStateMachineEnter is called when entering a state machine via its Entry Node
//override public void OnStateMachineEnter(Animator animator, int stateMachinePathHash)
//{
//
//}
// OnStateMachineExit is called when exiting a state machine via its Exit Node
//override public void OnStateMachineExit(Animator animator, int stateMachinePathHash)
//{
//
//}
}
Inventory.Item[0] for debug purposes is “fBall” if you were wondering