- Home /
Can someone reshape my script so that it is more clean and tidy? it works just looks ugly
'using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DisplayColor : MonoBehaviour {
//public Button bigButton;
public Button smallButton1;
public Button smallButton2;
public Button smallButton3;
public Button smallButton4;
public Button smallButton5;
public Button smallButton6;
public Button smallButton7;
public Button smallButton8;
public Button smallButton9;
public Button smallButton10;
public Button smallButton11;
public Button smallButton12;
public Button smallButton13;
public Button smallButton14;
public Button smallButton15;
public Button smallButton16;
public Button smallButton17;
public Button smallButton18;
public Button smallButton19;
public Button smallButton20;
public Button smallButton21;
Color[] colors;
// Use this for initialization
void Start () {
colors = new Color[]{smallButton1.image.color, smallButton2.image.color, smallButton3.image.color,smallButton4.image.color,smallButton5.image.color,smallButton6.image.color,smallButton7.image.color,smallButton8.image.color,smallButton10.image.color,
smallButton11.image.color,smallButton12.image.color,smallButton13.image.color,smallButton14.image.color,smallButton15.image.color,smallButton16.image.color,smallButton17.image.color,smallButton18.image.color,smallButton19.image.color,smallButton20.image.color, smallButton21.image.color };
GetComponent<Image>().color = colors[Random.Range(0, colors.Length - 1)];
}
}
I'm not sure exactly what you're trying to accomplish but try creating a public Button[] buttonArray OR a game object array and turn your Color[] to a public Color[]
Now look in the inspector where the script is supposed to be.. Now you can set the size of the array you'd like and drag your prefab Buttons into the array slots.. Same thing for the color's you'd like to use.
Try that and see how it works with what you're trying to do.
Actually I just want to write "public small buttons " in array form , so that they don't looks like this in inspector.
Answer by zeppike · Jun 04, 2015 at 04:48 PM
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class DisplayColor : MonoBehaviour {
public Button[] buttons;
List<Color> colors;
void Start () {
foreach (Button aButton in buttons){
colors.add(aButton.image.color);
}
GetComponent<Image>().color = colors[Random.Range(0, colors.Length - 1)];
}
}
How about:
public Button[] buttons;
void Start () {
foreach (Button aButton in buttons){
aButton.GetComponent<Image>().color = colors[Random.Range(0, colors.Length - 1)];
}
}
Nice touch. Gave me a nice idea :)
public Button[] buttons;
void Start () {
GetComponent<Image>().color = buttons[Random.Range(0, buttons.Length - 1)].GetComponent<Image>().color;
}
Answer by ROMgame · Jun 04, 2015 at 09:09 PM
This should work fine:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class DisplayColor : MonoBehaviour
{
//public Button bigButton;
public Button[] buttons;
// Use this for initialization
void Start () {
GetComponent<Image>().color = buttons[Random.Range(0, buttons.Length - 1)].image.color;
}
}
Yes @Abhi94, it works like charm, and the same code was posted 4 hours earlier, in the first answer.
Your answer
Follow this Question
Related Questions
Variable Vs Array? 2 Answers
creating prefabs in array 2 Answers
instantiating prefabs from array 1 Answer
how to instantiate gameobjects in a 2d array. 2 Answers
Add arrays to a big array? 2 Answers