[SOLVED] Source image not changing at runtime

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

Sounds like it’s not even the thing you think it is.

Press run, press pause, try change it by hand.

If that fails, delete it, then unpause. Who complains? What code is running? etc.

Again, just debugging 101… lots of debugging. Be critical and expect all of your code to be wrong and go prove that it isn’t wrong.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log() to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

I don’t know what I think it is at this point

I did it and it only changes the name of the sprite, when I unpause it it changes back to the previous one before runtime

I don’t know what you want me to delete, I deleted the component, then deleted the object. they were deleted. and brought back after I unpaused it

that’s what I did before posting

nothing wrong is happening. especially since there are no errors

I have reason to suspect its not the scripts that are the problem

the logs I used to have in the first script and the one I still have in the parent proved it was running.

I still can’t figure out what the problem is. This is truly baffling.

the last thing im gonna try is disable the parent script, or see what happens in the build

I (almost) Solved it! its the animator in the child object! I disabled the animator and I set the right sprite. I didn’t think it would affect anything because the animation doesn’t change the sprite and only the color. coming up with a solution now