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 /
  • Help Room /
avatar image
0
Question by Vivaldin · Jun 25, 2017 at 09:52 PM · arrayrandomrandom.rangearraylist

how to randomly choose a value from array

Evening.

I need to create an array - a bunch of colors hexes, and randomly choose one from the array. Ive tried to google - but all i've found is from 2013 tops, and not seem to work anymore.

At this point - how to create an array nor how to roll from it - is complete mystery. Please help.

Comment
Add comment · Show 1
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 Vivaldin · Jul 08, 2017 at 03:22 PM 0
Share

Here what i got so far

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Color$$anonymous$$ap : $$anonymous$$onoBehaviour {
 
     public string ColorString1;
     public string ColorString2;
     public string ColorString3;
     public string ColorString4;
     public string ColorString5;
     public string ColorString6;
 
     public Color Color1;
     public Color Color2;
     public Color Color3;
     public Color Color4;
     public Color Color5;
     public Color Color6;
 
     public void Start()
     {
         ColorString1 = "#e8f5e9";
         ColorString2 = "#00c853";
         ColorString3 = "#69f0ae";
         ColorString4 = "#ffffff";
         ColorString5 = "#00e676";
         ColorString6 = "#b2ff59";
 
         ColorUtility.TryParseHtmlString(ColorString1, out Color1);
         ColorUtility.TryParseHtmlString(ColorString2, out Color2);
         ColorUtility.TryParseHtmlString(ColorString3, out Color3);
         ColorUtility.TryParseHtmlString(ColorString4, out Color4);
         ColorUtility.TryParseHtmlString(ColorString5, out Color5);
         ColorUtility.TryParseHtmlString(ColorString6, out Color6);
 
         public Color[] ColorArrayBlue = new Color[] { Color1, Color2, Color3, Color4, Color5, Color6 };
 
     }
 }

It does not work. And the errors are not understandable at all =(

3 Replies

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

Answer by Vivaldin · Jul 08, 2017 at 04:27 PM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ColorMap : MonoBehaviour {
 
     public Color[] ColorArrayBlue;
 
     public string ColorString0;
     public string ColorString1;
     public string ColorString2;
     public string ColorString3;
     public string ColorString4;
     public string ColorString5;
 
     public Color Color0;
     public Color Color1;
     public Color Color2;
     public Color Color3;
     public Color Color4;
     public Color Color5;
 
     public void Start()
     {
         ColorString0 = "#e8f5e9";
         ColorString1 = "#00c853";
         ColorString2 = "#69f0ae";
         ColorString3 = "#ffffff";
         ColorString4 = "#00e676";
         ColorString5 = "#b2ff59";
 
         ColorUtility.TryParseHtmlString(ColorString0, out Color0);
         ColorUtility.TryParseHtmlString(ColorString1, out Color1);
         ColorUtility.TryParseHtmlString(ColorString2, out Color2);
         ColorUtility.TryParseHtmlString(ColorString3, out Color3);
         ColorUtility.TryParseHtmlString(ColorString4, out Color4);
         ColorUtility.TryParseHtmlString(ColorString5, out Color5);
 
         ColorArrayBlue = new Color[6] { Color0, Color1, Color2, Color3, Color4, Color5 };
     }
 }
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 Kishotta · Jun 25, 2017 at 11:48 PM

Without knowing how you are storing your hexes, I can't be more specific than this. But this should be pretty straightforward.

 // Create the array, assuming your hex objects are of type "Hex"
 Hex[] hexes = new Hex[hexCount];

 // Populate the array
 for (int i = 0; i < hexCount; i++) {
     hexes[i] = /*however you reference your hexes*/;
 }

 // Get a random hex from the list by getting a random index number in the array range
 Hex RandomHex = hexes[Random.Range (0, hexCount)];

Comment
Add comment · Show 3 · 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 NoseKills · Jun 26, 2017 at 01:55 PM 0
Share
 Hex RandomHex = hexes[Random.Range (0, hexCount - 1)]; // hexCount-1 because Range is inclusive

With floats it is inclusive, with ints it is not. So you shouldn't subtract 1 here.

avatar image Kishotta · Jun 27, 2017 at 06:02 PM 0
Share

@Nose$$anonymous$$ills is correct. I've updated my answer.

avatar image Vivaldin · Jul 08, 2017 at 03:17 PM 0
Share

Ive tried this one but the system tell me it cant understand the "Hex[]" I have to use some specific library for this?

avatar image
0

Answer by Vivaldin · Jun 27, 2017 at 03:52 PM

Can i define the array somehow clearly?

is there something like this? ColorList = new Array (1111, 2222, 3333, 4444, 5555...) ?

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 Kishotta · Jun 27, 2017 at 06:04 PM 0
Share

You can with:

 Color[] ColorArray= new Color[] {
     Color.white,
     Color.blue,
     new Color (0.5f, 3.1415926f, $$anonymous$$athf.Sqrt(2), 0.1f),
     Color.red
 }
avatar image Vivaldin Kishotta · Jul 08, 2017 at 03:19 PM 0
Share

This is not working. I need to use hex codes of the colors. If i use color[] - i have to use RGB colors i guess. I have no idea of how to transform the hex in to RGB by code...

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

78 People are following this question.

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

Related Questions

Why doesn't random range work? 1 Answer

Disabling random GUI Button from array 0 Answers

Color Randomize when object spawn help 0 Answers

Choose random object from array not the same as the randomly chosen object before 1 Answer

changing the color of a random game object not working 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