Reputation: 11
I have unity project and I used five toggle and set 1 image for the background another image to Checkmark. I want to set that both images by using code(C#).
Upvotes: 0
Views: 1296
Reputation: 11
Thank you for every ones to support me and I find a answer to this question. answer is:
first we need to declare varibles(for reference to Image) and then set images to variable from unity.
public Sprite MondayRW;
public Sprite MondayRG;
public Sprite TuesRW;
public Sprite TuesRG;
public Sprite WendRW;
public Sprite WendRG;
public Sprite ThurRW;
//If Phone language is Russian, Set Russian value images to tonggles.
if(Application.systemLanguage == SystemLanguage.Russian)
{
GameObject.Find("BackgroundMon").GetComponent<Image>().sprite = MondayRW;
GameObject.Find("CheckmarkMon").GetComponent<Image>().sprite = MondayRG;
GameObject.Find("BackgroundTue").GetComponent<Image>().sprite = TuesRW;
GameObject.Find("CheckmarkTue").GetComponent<Image>().sprite = TuesRG;
GameObject.Find("BackgroundWed").GetComponent<Image>().sprite = WendRW;
GameObject.Find("CheckmarkWed").GetComponent<Image>().sprite = WendRG;
GameObject.Find("BackgroundThu").GetComponent<Image>().sprite = ThurRW;
}
I find the place to replace my image from GameObject.Find("BackgroundMon")
and then I set my image to that place.
Upvotes: 0
Reputation: 82
I believe this is what you are looking for:
Image myImageComponent;
public Sprite myImage; //Drag your first sprite here in inspector.
void Start() //Lets start by getting a reference to your image component.
{
myImageComponent = GetComponent<Image>(); //Your image component is the one attached to this gameObject.
}
public void SetImage() //method to set our first image
{
myImageComponent.sprite = myImage;
}
Upvotes: 1