Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm writing a script to change the image on the button based on previous selection. Another script is giving the CurrentAvatar variable once previous button is pressed.

My problem is i can't seem to load an image for the button using:

AvatarSprite = Resources.Load<Sprite>("Assets/Images/FireMonsters/Lamor_Icon");

Could someone please enlighten me on the proper code?

All from assets, or is it even possible?

Also looking to have multiple options in if statements, don't know a better method.


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

public class SelectedAvatar : MonoBehaviour {

    Button CurrentHeroSprite;
    static Sprite AvatarSprite;
    public static string CurrentAvatar;

    // Use this for initialization
    void Start () {
        CurrentHeroSprite = GetComponent<Button>();
    }

    // Update is called once per frame
    void Update () {
        if (CurrentAvatar == "Lamor") {
            AvatarSprite = Resources.Load<Sprite>("Assets/Images/FireMonsters/Lamor_Icon");
            CurrentHeroSprite.image.sprite = AvatarSprite; 
            Debug.Log ("loaded sprite for Lamor");
        }
        //if etc etc etc
    }
}
share|improve this question
1  
Where is Lamor_Icon located in your folder structure? Resources.Load can only be used to load assets that are in a folder called "Resources" (this marks the assets as needing to be serialized for loading on demand from script), and the path you give needs to be relative to that Resources folder. –  DMGregory Jun 5 at 12:18
    
I was wondering the same thing. The path Assets/Images/FireMonsters/Lamor_Icon looks like the root of your project, rather than inside a Resources folder. –  jhocking Aug 4 at 15:13

1 Answer 1

If you're having trouble linking the sprite in your script, why don't you just assign your script to the sprite itself? Instead of : Resources.Load<Sprite>("Assets/Images/FireMonsters/Lamor_Icon"); make a generic script, that will GetComponent<Sprite> from the sprite you assign it to, add that sprite to your hierarchy, and run your code then? Or just add the sprite to your scene and instead of using Resources.Load you could use GetComponent<SpriteRenderer>();

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.