Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 · Jun 04, 2015 at 02:53 PM · arraysscriptingbasics

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)];
          
      }
      
  }

Comment
Add comment · Show 2
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 SterlingSoftworks · Jun 04, 2015 at 04:00 PM 0
Share

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.

avatar image Abhi94 · Jun 04, 2015 at 04:39 PM 0
Share

Actually I just want to write "public small buttons " in array form , so that they don't looks like this in inspector.

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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)];
           
       }
       
   }
  
Comment
Add comment · Show 2 · 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 fafase · Jun 04, 2015 at 05:07 PM 1
Share

How about:

    public Button[] buttons;

    void Start () {
         foreach (Button aButton in buttons){
              aButton.GetComponent<Image>().color = colors[Random.Range(0, colors.Length - 1)];
         }
    }
avatar image zeppike · Jun 04, 2015 at 05:13 PM 0
Share

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;
 }
 
avatar image
0

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;
           
       }
       
   }
Comment
Add comment · Show 2 · 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 · Jun 05, 2015 at 07:03 AM 0
Share

Thanks RO$$anonymous$$game it worked great

avatar image zeppike · Jun 05, 2015 at 07:55 AM 0
Share

Yes @Abhi94, it works like charm, and the same code was posted 4 hours earlier, in the first answer.

avatar image
0

Answer by Abhi94 · Jun 05, 2015 at 08:24 AM

thank you all you for your help

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

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


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