How to choose a random preference (complicated)
Before the game starts, the player has 5 preferences, a b c d and e, and he can enable and disable as many as he wants. For example
a - enabled b - disabled c- enabled d - enabled e - disabled
When the player enters a trigger, a random preference (a b c d or e) is chosen. If the preference is disabled, this preference won't be put into the random choice. Since there are so many different combinations the user can have, is there a simple way to do this other than writing out every single possible combination that the user may have chosen? I know I will use ontriggerenter and Random.Range, but not sure where to go from there
Thanks
I'm not sure i understand your problem correctly.
You have let's say 5 options A,B,C,D,$$anonymous$$ Let's say they are all represented by gameObjects.
First the player can disable any of them. After that he enters the trigger of one of the still enabled objects, and at that point you want to get a list of all the other still enabled objects ?
Is that it?
Answer by KdRWaylander · Nov 19, 2015 at 12:16 PM
Hi,
On trigger enter, you want your app to compute 5 times a little routine to set randomly your preferences ? Are those references booleans ? Or ints/floats ?
If it's a boolean choice, here's a way to do this:
if (preference is disabled) → return;
else: throw a random value
and : if random value is > to .5f set preference to true, else set to false
apply new value to preference
Now you loop in all your preferences with a foreach :)
If it's not a boolean choice, but floats, use random.range and set the output value to your preference or make a switch case on a random.range with integers
Hey thanks for you answer, I decided to add the enabled options to a list at the start and then choose a random one from the list
If you just want a random bool-combination for a, b, c, d, and e you can use something like this:
byte m_combination;
bool a;
bool b;
bool c;
bool d;
bool e;
public void RandomCombination()
{
m_combination = (byte)Random.Range(0, 32);
if ((m_combination & (byte)1) == (byte)1) a = true;
else a = false;
if ((m_combination & (byte)2) == (byte)2) b = true;
else b = false;
if ((m_combination & (byte)4) == (byte)4) c = true;
else c = false;
if ((m_combination & (byte)8) == (byte)8) d = true;
else d = false;
if ((m_combination & (byte)16) == (byte)16) e = true;
else e = false;
//Debug.Log(a + " " + b + " " + c + " " + d + " " + e);
}
Your answer
Follow this Question
Related Questions
How to carry over button sound to another scene 1 Answer
NullReferenceException: Error from the GetStyle() - Method 1 Answer
Rayscast from the GameObject that is not the player does not work 0 Answers
UnityAction Vs UnityEvent 1 Answer
Error `UnityEngine.Component' does not contain a definition 1 Answer