- Home /
I used another approach to solve the same problem. Question closed to avoid unecessary notifications.
Trouble creating reference of image in script.
I'm trying to do this:
private Image image;
//On Start I wrote:
image = new Image();
Well, it doesn't work. I get a "NullReferenceExeption" (if there's any annoying situation, then the null reference is on top of the list).
Unity also says I can't use it because of the level of protection of Image.Image(). I've got a look at it, and it's literaly protected.
Any ideas on how to solve this? Thanks in advance.
Answer by ShadyProductions · Aug 17, 2017 at 05:01 PM
You can't since it's a monobehaviour, you must use AddComponent
AddComponent<Image>();
To set new sprite:
GetComponent<Image>().sprite = newSpriteImage;
Thanks, but now I'm using another approach. I needed this to make my game accept multiple texture packs, and I wasn't able to change the button's image sprite to match the icons contained in the texture packs. But i solved it.
Thanks, anyway.
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object 1 Answer
NullReferenceException: Object reference not set to an instance of an object ? 1 Answer
Getting a reference to a group of ScriptableObjects implemented at runtime? 0 Answers
Easily reference class instance 1 Answer
The infamous: Object Reference not set to an instance of an object 1 Answer