- Home /
Unity 4.6: How to access Image class? Let's say to change sprite with the code.
Well these features are pretty new I would say this is just a bug waiting to be fixed. But it's weird that Image class is shown as "Image (script)". And when in code there is no class Image showing up or maybe I am missing something, maybe there is another way to access this class.
The puzzle I'm trying to solve is: what's the code to access "source Image"?
Answer by Gurunext · Aug 23, 2014 at 10:24 AM
Ok I was able to quickly answer my own question.
I needed to add this... obviously
using UnityEngine.UI;
LOL was stuck on this for hours. Clutch man thanks.
Answer by SoloSebo · Sep 01, 2014 at 11:00 PM
For others that stumble upon this (and still dont get it like me...), here is a little more meat.
var theImage:UnityEngine.UI.Image;
var theSprite:Sprite; //The image you want to drag in the inspector
theImage.sprite = theSprite; //Setting the new image
Hopefully I didn't complicate things.
Do you know hot to do it without a reference to the sprite? For example having the sprite name in a string and creating the sprite so you can dynamically change images depending on an object's property?
Something like this:
Public string ObjectName;
Public Image ObjectIcon;
void Start()
{
ObjectIcon.sprite = new Sprite("ICON_" + ObjectName);
}
The way I set the new image was through accessing the Image's material
var material = AssetDatabase.LoadAssetAtPath(path,typeof($$anonymous$$aterial)) as $$anonymous$$aterial; images[i].GetComponent"<"Image">"().material = material;
Excuse the double quotes around the "<" ">". those and all text inside them would automatically disappear without the ""
Your answer
![](https://koobas.hobune.stream/wayback/20220613161320im_/https://answers.unity.com/themes/thub/images/avi.jpg)