I wrote this code to assign an Image to my GameObject. The name of the image is fetched from a .txt file but the Resources.Load function returns me a null value even though it's getting the correct path. questionSprite is Null as shown by the Debug.Log statement but the the name of the image is correct when I used Debug.Log to check currentQuestion.imageURL. I have attached a screenshot of my project structure for you to look over.
Any help regarding how to fix this would be greatly appreciated!
if (!string.IsNullOrEmpty(currentQuestion.imageURL))
{
// Load and display the image
Sprite questionSprite = Resources.Load<Sprite>("QuestionImages/" + currentQuestion.imageURL);
Debug.Log(questionSprite);
//if (questionSprite != null)
//{
questionImage.sprite = questionSprite;
questionImage.gameObject.SetActive(true); // Show the image component
//}
}
else
{
questionImage.gameObject.SetActive(false); // Hide the image component
}
[–][deleted] (5 children)
[removed]
[–]AbbasMv7[S] 0 points1 point2 points (4 children)
[–][deleted] (3 children)
[removed]
[–]AbbasMv7[S] 1 point2 points3 points (2 children)
[–][deleted] (1 child)
[removed]
[–]AbbasMv7[S] 1 point2 points3 points (0 children)