- Home /
Randomizer code not working.
Am trying to build a randomizer code for use in a random generator i'm building for a friend (hence the various works referenced in the variables). It's meant to select from a preset array list based on the select variables whenever the random selection button is pressed, and i've got it to a point where it feels like the only two bits not working are the randomizer line itself and the output to an input field (which could easily fix itself if the first issue is solved). I'm not an efficient coder, so there's some repeated things, but I was wondering if anyone can see what i'm doing wrong with this at a cursory glance?
I wasn't sure what to cut out for ease of reading, so just know the actual randomizer code line itself is repeated for multiple different situations in the TaskOnClick function.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RandGenName : MonoBehaviour {
public Button nameButton;
public Toggle togMLP;
public Toggle togRWBY;
public Toggle tog40K;
public int checkMLP;
public int checkRWBY;
public int check40K;
public int checkTotal;
string resultText;
public Text resultOutput;
/*40K = 1*/ public List<string> rand1 = new List<string>();
/*MLP = 2*/ public List<string> rand2 = new List<string>();
/*RWBY= 4*/ public List<string> rand4 = new List<string>();
/*40K/MLP = 3*/ public List<string> rand3 = new List<string>();
/*40K/RWBY= 5*/ public List<string> rand5 = new List<string>();
/*MLP/RWBY= 6*/ public List<string> rand6 = new List<string>();
/*All = 7*/ public List<string> rand7 = new List<string>();
public void Start () {
tog40K.isOn = false;
togMLP.isOn = false;
togRWBY.isOn = false;
check40K = 0;
checkMLP = 0;
checkRWBY = 0;
checkTotal = 0;
resultText = "";
nameButton.onClick.AddListener (TaskOnClick);
}
public void Update () {
if (tog40K.isOn) {
check40K = 1;
}else{
check40K = 0;
}
if (togMLP.isOn) {
checkMLP = 2;
}else{
checkMLP = 0;
}
if (togRWBY.isOn) {
checkRWBY = 4;
}else{
checkRWBY = 0;
}
checkTotal = check40K + checkMLP + checkRWBY;
resultOutput.text = resultText;
//Debug.Log (checkTotal);
}
public void TaskOnClick () {
if (checkTotal == 1) {
string resultText = rand1[Random.Range(0, rand1.Count)];
resultOutput = GetComponent<Text>();
resultOutput.text = resultText;
Debug.Log (resultOutput);
}
if (checkTotal == 2) {
string resultText = rand2[Random.Range(0, rand2.Count)];
resultOutput = GetComponent<Text>();
resultOutput.text = resultText;
Debug.Log (resultOutput);
}
if (checkTotal == 3) {
}
if (checkTotal == 4) {
string resultText = rand4[Random.Range(0, rand4.Count)];
resultOutput = GetComponent<Text>();
resultOutput.text = resultText;
Debug.Log (resultOutput);
}
if (checkTotal == 5) {
}
if (checkTotal == 6) {
}
if (checkTotal == 7) {
}
}
}
From way out here it is nearly impossible to understand what you need to do by reading through the code, in part because there are no comments to document what you're thinking, and in part because even in your question I have little to go on as to what the plan is. I can't say what checkTotal means, for example. If it happens to be 3, 5, 6 or 7, nothing is going to happen, and I can't tell if that is or isn't the problem.
We need a more general overview of the plan, especially what is required. Note, that's not entirely an explanation of what you're writing in this code, but what you need to happen.
Ah, sorry. Should've been more clear. I wasn't really thinking about the documentation much, largely because I was originally going to be the only person who saw this.
The idea is that different numbers are output by checkTotal as a value based on which check boxes I have selected in my actual scene, and this affects which list the group selects from to randomly output a string (hence the 'if (checkTotal == x)' in the TaskOnClick function).
The 3, 5, 6 and 7 entries are fine. They will contain the same code as the existing three, or a variation on it. If it simplifies things for the purposes of working this out, assume that, in the game scene, checkTotal is permanently set to 1, because all the other numbers will function the same way if it works.
Answer by LCStark · Oct 06, 2018 at 01:03 PM
What exactly doesn't work with your code? string resultText = rand1[Random.Range(0, rand1.Count)];
is simple enough that it shouldn't fail (though I think you could have problems here if your rand1
list was empty). Split that code and check the values of your random numbers:
int count = rand1.Count;
Debug.Log("rand1 count: " + count);
int randNumber = Random.Range(0, count);
Debug.Log("random number: " + randNumber);
string resultText = rand1[randNumber];
Thanks for the input. I'll check this when I have a free moment.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do i Achieve mesh Extrusion? 2 Answers
Can I make a collider ignore only one other collider? 0 Answers
knockback 3d c# 1 Answer