Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
2 captures
11 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Solstyce10 · Jul 24, 2015 at 09:16 AM · unity 5uigetcomponentui image

Trying to change Source Image of UI Image via Script

Hello,

I have been working on trying to access this particular component and have not had any luck, this script works (no errors) but its not changing the Source Image component of the object the script is attached to.

Note: I am using a button to SendMessage(string) to run the Roll () method in this script.

The field I want access to change is:

alt text

Both highlighted fields should be the same.

Code is Below. Thanks in advance for the help!

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class diceImage : MonoBehaviour {
 
     public int rng;
     public Sprite diceImg; 
     public Sprite blank;
     public Sprite clear;
     public Sprite crit;
     public Sprite focus;
     public Sprite hit;
 
     void Awake () {
         diceImg = GetComponent<Image> ().sprite;
     }
 
     // Use this for initialization
     void Start () {
         diceImg = blank;
         rng = 0;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void Roll () {
 
         rng = Random.Range (1, 9);
         Debug.Log (rng);
 
         if (rng == 1) {
             diceImg  = clear;
         } else if (rng == 2) {
             diceImg  = crit;
         } else if (rng == 3) {
             diceImg = focus;
         } else if (rng == 4) {
             diceImg = hit;
         } else if (rng == 5) {
             diceImg = focus;
         } else if (rng == 6) {
             diceImg = hit;
         } else if (rng == 7) {
             diceImg = clear;
         } else if (rng == 8) {
             diceImg = hit;
         } else {
             diceImg = blank;
         }
     }
 }
 

 


sourceimage.jpg (89.4 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dhawalbanker_g · Oct 31, 2016 at 03:10 PM 0
Share

Does this answer still hold for Unity 5.x.x? There is no component named .

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Veldars · Jul 24, 2015 at 09:25 AM

Hi, you must assign your sprite directly on the image.

 GetComponent<Image>().sprite = newSprtie;

or

 Image img = GetComponent<Image>();
 img.sprite = newSprite;

I hope this help...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Solstyce10 · Jul 24, 2015 at 12:46 PM 0
Share

Thanks Veldars! This is the help I was looking for.

I changed every if and else if from this

if (rng == 1) { diceImg = clear;

To this

 if (rng == 1) {
             GetComponent<Image> ().sprite  = clear;
         }

I removed the diceImg variable completely.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do i make images visible/invisible c# 0 Answers

Horizontal Layout doesn't align to center if I disable images 0 Answers

Canvas not filling up the whole screen in Unity 5 0 Answers

Image attached to the image component in a button is not visible during play mode 0 Answers

Level Selector - Level Images for Brick Breaker 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges