- Home /
ScriptableObject , use Resources.Load sprite then it have error.
Hi all, I'm making quiz game with Image.
I create the ScriptableObject in "Resources" folder.
My anserButton is a prefab , so I use resources.load to get the scriptableObject. And everything is ok , when I want it to change sprite , it can't work. I don't know where do I missing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName ="SaveImage",menuName = "SaveImageSetting")]
public class QuestionImage : ScriptableObject
{
public Sprite image;
}
AnsButton.cs
public Image image;
public QuestionImage loadImage; ///First,this is null
void Start()
{
loadImage = (QuestionImage)Resources.Load("" + answerText.text + "", typeof(QuestionImage));
}
public void Setup(AnsData data)
{
ansData = data;
answerText.text = ansData.ansText;
gameObject.GetComponent<Image>().sprite = answerColor[0];
isCorrect = ansData.isCorrect;
gameObject.GetComponent<Button>().interactable = true;
if (PlayerPrefs.GetString("NowScene") == "Game3")
{
image.enabled = true;
image.sprite = loadImage.image; ///the error is here,but I don't know why.
}
}
Are you sure the resource exists and is correctly located at the given path? Note that it needs to be in a "Resources" folder to work correctly.
Is it possible that the "Setup" function is called before the Start function? If you call it from an "Awake" function or the "Start" function of another script, it's possible that the Start function from this script is not called yet, and loadImage is still null.
Thank you for you reply ! I think is 2. I will try latter :)
Seeing the error you get would make it easier to analyze your problem
I solved the problem , Like @SirPaddow says, the problem is 2. I have another script call early, so it not called yet . Thank you for your reply :)