[SOLVED] C# - Fetching Sprite via Script

Okay, so. I have a magic system in my game where you cast spells by stringing together different elements and hitting Cast!. Right now I’m working on the buttons the player will click to ‘load’ elements into their current spell. I have a button that has all the functionality I want, and even loads the proper element into the current casting, so that part’s good.

What I’m stuck on is giving the buttons the right image. I’m instantiating the buttons for all possible elements the player knows from a single button, and then want to assign it an image based on the element assigned to that button. I’m already doing something similar by assigning it the element’s effect, but I’m having trouble fetching the right image, I think. So basically here’s what I have:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Element {

    public string Name;
    public string Desc;
    public int Cost;
    public Sprite Icon;

    public virtual void Effect () {
        Debug.Log ("You used " + Name + "!");
    }

}

This is my generic Element class from which other elements inherit. Here’s an example of one of the elements:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Fire : Element {

    public GameObject ComMan;
    CombatManager manager;

    // Use this for initialization
    void Start () {
        ComMan = GameObject.FindWithTag ("CombatManager");
        manager = ComMan.GetComponent<CombatManager> ();
        Name = "Fire";
        Desc = "The destructive force of fire.";
        Cost = 1;
    }
   
    public override void Effect () {
        manager.DealDamage (5);
    }
}

So this element, fire, deals 5 damage when used in a spell. That part works great. But what I need it to do is also set the Icon for fire to the appropriate sprite (currently stored in Resources/Elements/fire). That’s the line I’m missing so that I can do this later on:

    void CreateHandButton(Element ele) {
        Button handbutton = (Button)Instantiate (Resources.Load<Button> ("HandButton"), PHand.transform.position, Quaternion.identity);
        handbutton.GetComponent<Image> ().sprite = ele.Icon;
        handbutton.transform.SetParent (PHand.transform, false);
        handbutton.transform.localPosition = new Vector3 (x, y, 0);
    }

I’m just not sure how to get the fire icon image via code, I guess. Please help!

I’ve been tinkering away at this and it occurred to me that I don’t think the Fire element actually runs Start(). I’ve changed the code for it to this so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Fire : Element {

    public GameObject ComMan;
    CombatManager manager;

    // New instances of Fire element
    public Fire () {
        Name = "Fire";
        Desc = "The destructive force of fire.";
        Cost = 1;
        Icon = Resources.Load ("Elements/fire") as Sprite;
        Debug.Log ("Made fire!");
    }
  
    public override void Effect () {
        ComMan = GameObject.FindWithTag ("CombatManager");
        manager = ComMan.GetComponent<CombatManager> ();
        manager.DealDamage (5);
    }
}

It does let me know when the player loads in that it made the player’s instance of the fire element (debug prints Made fire!). But it’s still not setting the image on the button. I’m at a loss where to go from here. :frowning:

Also just added a debug for if (Icon != null) and it doesn’t print…so Icon is null still.

AH! I found what I needed here!

I was really close - the correct code is:

    // New instances of Fire element
    public Fire () {
        Name = "Fire";
        Desc = "The destructive force of fire.";
        Cost = 1;
        Icon = Resources.Load<Sprite> ("Elements/fire");
        Debug.Log ("Made fire!");
        if (Icon != null) {
            Debug.Log ("Found an icon!");
        }
    }