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 Kebabs · Nov 30, 2013 at 04:00 PM · randomarraysrandom.rangebooleans

An array of bools with random true and false elements

Hi

I have made a built in array of bools and I was wondering if anyone knew of a way to use random.range or any other method to switch the true statement into different elements slots in the array.

     testArray2 [0] = true;
     testArray2 [1] = false;
     testArray2 [2] = false;
     testArray2 [3] = false;

so at the moment [0] is true I was wondering if there is a way to randomly switch the single true statement to say [1] and keep all the others false. That way I can have several If statements that can say...

     if (testArray2[0])
     {
         print ("working");
         gameObject.renderer.material = Red;
         gameObject.tag ="redEnemy";
     }
     
     if (testArray2 [1])
     {
         gameObject.renderer.material = Blue;
         gameObject.tag ="blueEnemy";
     }
     
     if (testArray2 [2])
     {
         gameObject.renderer.material = Green;
         gameObject.tag ="greenEnemy";
     }
             
     if (testArray2 [3])
     {
         gameObject.renderer.material = Yellow;
         gameObject.tag ="yellowEnemy";
     }

and this is how I can make it random (if the array was an INT)

     System.Random random = new System.Random();
     randomTest = testArray2[random.Next(0, testArray2.Length)];

Any help is greatly appreciated as this is uni coursework. Thank you kindly.

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 robertbu · Nov 30, 2013 at 04:05 PM 1
Share

It is unclear what you are asking. The line of code you list...

 randomTest = testArray2[random.Next(0, testArray2.Length)];

...world work fine with an array of booleans.

When you say, "switch the true statement into different elements slots in the array," it implies you want to shuffle the elements of the array. Or perhaps you just want to give each entry a random value?

 for (var i = 0; i < testArray2.Length; i++) 
     testArray2[i] = (Random.Range(0,2) == 1);

2 Replies

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

Answer by Bunny83 · Nov 30, 2013 at 04:03 PM

Sure, don't use an array at all. It makes no sense in this case. Just use a single integer variable to specify which statement you want to execute:

 int selection = 0;
 // [...]
 if (selected == 0)
 {
     // ...
 }
 else if (selected == 1)
 {
     // ...
 }
 else if (selected == 2)
 {
     // ...
 }

edit

In your case it seems you want to distinguish different enemy types. Usually you simply create a prefab for each enemy type and put the prefabs in an array from which you select a certain type to be instantiated.

However if you just want to set a Material and a tag you can use a custom class like this:

 [System.Serializable]
 public class EnemyType
 {
     public string tag;
     public Material material;
 }

Inside you actual enemy MonoBehaviour you could declare an array like this:

 public EnemyType[] enemyTypes;
 public int enemyType = 0;
 
 void Start()
 {
      renderer.material = enemyTypes[enemyType].material;
      gameObject.tag = enemyTypes[enemyType].tag;
 }

However seperate prefabs are usually much more easier to maintain and to work with. But without more information on the purpose of your code snippet we can't say much more about this.

Comment
Add comment · Show 1 · 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 Kebabs · Nov 30, 2013 at 05:02 PM 0
Share

Thank you all for your advice and help and if I was able to upvote I would. Bunny83 your answer made the most sense and my code is now working as intended - so thank you very much :).

And everyone else kind regards once again. Advice like this encourages me to continue improving my coding skills and achieve the best I can at uni. Thank you :)

avatar image
0

Answer by Owen-Reynolds · Nov 30, 2013 at 04:26 PM

Since you write it's for a real school course, you may as change the if into a lookup (too confusing for many programmer/artists, but in school you'll learn it anyway):

 public Color[] EnemyCols; // set in Inspector
 public string[] EnemyTags = {"redE", "greenE", ... };

 int selected = 0; // single-var trick from Bunny's reply

 // replaces all of the ifs:
 gameObject.renderer.material = EnemyCols [ selected ];
 gameObject.tag = EnemyTags [ selected ];
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

18 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

Related Questions

How to exclude int values from Random.Range? 2 Answers

pick a random int with the value of 1 from an array 2 Answers

How do you get different random numbers for each object array? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to make particles randomized each run? 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