- Home /
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);
}
}
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;
}
}
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
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 .
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;
}
}
}
}
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?
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 .
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.
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