Search Unity

Changing the Panel Source Image through code

Discussion in 'Getting Started' started by carlsonz16, May 18, 2020.

  1. carlsonz16

    carlsonz16

    Joined:
    May 13, 2020
    Posts:
    3
    Hi,

    I am trying to change the source image of the panel (my background) through code. In my game I have upgrades that will add stuff to the background image. I have a bunch of background images to cycle through but I can't seem to find anything online about how to change the source image of the panel background through code. Here's what I have so far.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using System.Globalization;
    public class Background : MonoBehaviour
    {
    public Sprite Scene2;
    public void Change()
    {
    if (MoneyPerSecond.level == 2)
    {
    GameObject.Find("Panel").GetComponent<Image>().sprite = Scene2;
    }
    }
    }


    Thanks
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    Everything looks right. Just make certain you have the actual asset set to type "Sprite" in the import settings, and that you've linked it into the Inspector.
     
  3. carlsonz16

    carlsonz16

    Joined:
    May 13, 2020
    Posts:
    3
    I think I did that and I am getting this error:

    UnassignedReferenceException: The variable spriteRenderer of MoneyPerSecond has not been assigned.
    You probably need to assign the spriteRenderer variable of the MoneyPerSecond script in the inspector.
    MoneyPerSecond.Upgrade () (at Assets/Scripts/MoneyPerSecond.cs:24)
    UnityEngine.Events.InvokableCall.Invoke () (at <b53f6837a4614744b595be69da4cbb29>:0)
    UnityEngine.Events.UnityEvent.Invoke () (at <b53f6837a4614744b595be69da4cbb29>:0)
    UnityEngine.UI.Button.Press () (at C:/Program Files/Unity/Hub/Editor/2020.1.0b4/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
    UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2020.1.0b4/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
    UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Program Files/Unity/Hub/Editor/2020.1.0b4/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
    UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/Program Files/Unity/Hub/Editor/2020.1.0b4/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
    UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Hub/Editor/2020.1.0b4/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:376)