Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Abhi94 · Aug 30, 2015 at 09:11 PM · texturearraycolorgetcomponent

How to access multiple components of a component at once?

**I want my function to pick texture and color of the same RawImage from the random array of RawImages. But i can't seem to do that .

I have tried accessing both texture and color separately but due it i get color from another RawImage and texture from another . So please kindly help me.**

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class DisplayColor7 : MonoBehaviour {
 
 
     public RawImage[] buttons;
     public RawImage DeadPool;
 
     public    void Start () {
         
         
         changeColor ();
         
         
     }
     
     
     public    void changeColor() {
     
 
         GetComponent<RawImage>().texture&Color = buttons [Random.Range (0, buttons.Length - 1)].texture&Color; 
 
         
         //DeadPool = this. buttons [Random.Range (0, buttons.Length - 1)];
     
         //DeadPool = new RawImage(buttons [Random.Range (0, buttons.Length - 1)].mainTexture);
 
  }
 }

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

2 Replies

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

Answer by Mikilo · Aug 31, 2015 at 09:44 AM

Hello!

If I correctly understand you, you want to take the Texture and Color of DeadPool from buttons.

To achieve this your can do like that:

     public class DisplayColor7 : MonoBehaviour
     {
         public RawImage[]    buttons;
         public RawImage        DeadPool;

         public void    Start()
         {
             changeColor();
         }

         public void    changeColor()
         {
             int    randomValue = Random.Range(0, buttons.Length - 1);

             DeadPool.color = buttons[randomValue].color;
             DeadPool.texture = buttons[randomValue].texture;
         }
     }
Comment
Add comment · Show 11 · 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 Abhi94 · Aug 31, 2015 at 09:37 AM 0
Share
          GetComponent<RawImage>().Color = buttons [Random.Range (0, buttons.Length - 1)].Color       
 
   GetComponent<RawImage>().texture = buttons [Random.Range (0, buttons.Length - 1)].texture; 

You mean like this , thanks mate but I have already tried it . It works but it picks Color and texture from different RawImages rather a one single random RawImage

avatar image Abhi94 Abhi94 · Sep 02, 2015 at 01:23 PM 1
Share

Hey, Sorry for this late reply $$anonymous$$ikilo and also i am very very grateful to you my friend your last code worked like a charm. And for that i am very thankful to you .

avatar image Abhi94 · Aug 31, 2015 at 04:17 PM 0
Share

Thank you $$anonymous$$ikilo but It is only picking color from random RawImages not texture from those images . I want to pick both texture and color from one single random image .

 public class DisplayColor7 : $$anonymous$$onoBehaviour {
     
     
         public RawImage[] buttons;
         public RawImage DeadPool;
     
         public    void Start () {
             
             
             changeColor ();
             
             
         }
         
         
         public    void changeColor() {
         
     
             DeadPool.GetComponent<RawImage> ().color = buttons [Random.Range (0, buttons.Length - 1)].color;
     
     
     
             for (int i = 0; i < buttons.Length; i++)
             {
                 if (buttons[i].GetInstanceID() == this.DeadPool.GetInstanceID())
                 {
                     DeadPool.GetComponent<RawImage> ().texture = buttons [Random.Range (0, buttons.Length - 1)].texture;
                 }
             }
     
      }
     }


avatar image Mikilo · Aug 31, 2015 at 04:29 PM 0
Share

Hum... Are you saying that you want something like this:

     public void    changeColor()
     {
         int    randomValue = Random.Range(0, buttons.Length - 1);

         DeadPool.GetComponent<RawImage>().color = buttons[randomValue].color;

         for (int i = 0; i < buttons.Length; i++)
         {
             if (buttons[i].GetInstanceID() == this.DeadPool.GetInstanceID())
             {
                 DeadPool.GetComponent<RawImage>().texture = buttons[randomValue].texture;
                 break;
             }
         }
     }

I have saved the random value in a variable, then I use it to pick from the same RawImage in buttons.

I need to say, that at this point, I still dont understand the question.

Can you explain what should be the link between DeadPool and buttons?

avatar image Abhi94 · Aug 31, 2015 at 04:41 PM 0
Share

Sure I will try to explain you Actually in my scene there are 10 raw images that will randomly change there texture and Color . And those 10 raw images are "buttons ".

And DeadPool is like a primary image which picks texture and Color from those 10 random raw images . But now the problem is that I want my DeadPool Rawimage to pick Color and texture of a same RawImage ( from the buttons) . But it picks Color form different RawImage and texture form a Different RawImage .

avatar image Mikilo · Sep 02, 2015 at 01:29 PM 1
Share

Finally, 48h after, here we are! That's good to hear.

You are welcome. But since you are new, you need to know some rules. Don't create topics in every corners of Unity Answers, make one, but make it clear, that way we will be more able to help you.

Also, when a solution has been found, you should mark it as accepted answer, it makes the topic clean and neat, and easier for future users to rapidly watch over the topic.

Show more comments
avatar image
0

Answer by Dinosaurs · Aug 31, 2015 at 04:21 AM

You should store the random button and the RawImage component as variables and assign to the texture and color on two separate lines. It seems like you might want to check out the Unity documentation on variables to figure this out. https://unity3d.com/learn/tutorials/modules/beginner/scripting/variables-and-functions

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

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

I'm trying to change a material, texture or color and it doesnt work 0 Answers

I can't see a reason for this Array.Reverse() error 3 Answers

if for array 1 Answer

Loop trough list of textures and chek mouse position over 0 Answers

Making a GetComponent array 1 Answer


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