Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
2
Question by Marshal_Alessi · Dec 31, 2014 at 07:18 PM · c#canvasimage

Changing Unity.UI.Panel Color

How would one go about doing that? My method did not work.

                 Image img =    transform.Find("UpgradeCanvas/Speed/First").GetComponent<Image>().color = Color.blue;
 


It says it could not convert Unity.Color to Unity.UI.Image, but I am otherwise unsure of how to change this via script.

Comment
Add comment · Show 4
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 Marshal_Alessi · Dec 31, 2014 at 07:05 PM 0
Share

I can't believe I did that. Okay, so now I moved it down to img.color = color.red; but that still isn't actually changing the color. Any idea why?

avatar image Qasem2014 · Dec 31, 2014 at 07:39 PM 0
Share

test it :

 Component img= transform.Find("UpgradeCanvas/Speed/First").GetComponent<Image>();
 
 img.color = Color.blue;
avatar image Qasem2014 · Dec 31, 2014 at 07:40 PM 0
Share

is "UpgradeCanvas/Speed/First" a game object name ? :-?

avatar image Marshal_Alessi · Dec 31, 2014 at 09:00 PM 0
Share

It should be Image img, not component. It exists, so I don't know why it didn't work. I changed it to a public variable and just dragged it in, and now it works.

3 Replies

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

Answer by code_warrior · Jan 01, 2015 at 12:53 PM

Hi Marshal_Alessi,

I've just checked out your question in a real simply Unity Scene, I had one UI Panel Called MyPanel.

The working C# script:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ChangePanel : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         Image img =  GameObject.Find("MyPanel").GetComponent<Image>();
         img.color = UnityEngine.Color.red;
     }
 
 }

If you assign the script directly to your Panel you don't need the GameObject.Find() Method, simply call the GetComponent function.

All you now have to do is to change the color and the name of your object. Please note: Its essential to do the GetComponent and the process of assigning a color in two separate lines.

code_warrior

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 jrbaxendale · Oct 06, 2020 at 10:20 AM 0
Share

$$anonymous$$ake sure you actually have 'using UnityEngine.UI;' in your script; $$anonymous$$ine was missing and the code would not work until I spotted it.

avatar image
1

Answer by DevsGoingViral · Jun 15, 2015 at 06:09 PM

Working js script if someone need:

 #pragma strict
 import UnityEngine; 
 import System.Collections; 
 import UnityEngine.UI;
 ///////////////////////
 var imgObj : GameObject;  //drag your Canvas here
 var img : Image = imgObj.GetComponent.<Image>(); //drag your image here
 function Start () {}
 
 function Update () {
 img.color = Color.red; //instant change
 }

Comment
Add comment · 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
-1

Answer by Jivibounty · Nov 24, 2018 at 11:36 PM

Your code assigns color to image. Hence cannot assign Unity.Color to Unity.UI.Image. That is the problem. remove "Image img =" .

THE CODE SHOULD BE

transform.Find("UpgradeCanvas/Speed/First").GetComponent().color = Color.blue;

AND NOT

Image img = transform.Find("UpgradeCanvas/Speed/First").GetComponent().color = Color.blue;

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 Owen-Reynolds · Nov 25, 2018 at 04:18 AM 0
Share

Look at the other answers. Green means the asker marked it as correct. It's more like what the person was asking. What you wrote could be a comment on that answer about doing it in one step ins$$anonymous$$d of 2.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Hotbar appears when a certain gameobject is selected? 1 Answer

Problem with Swap Image in Unity 5 with C#. 2 Answers

How to draw an Image between two objects within the canvas? 1 Answer

Change Canvas Image from Another Script 1 Answer

Scaling an image sent to UI Image 0 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