- Home /
How to use ArrayList.IndexOf and Conver.ToInt32
Hi all. I have 5 questions in my project also they have answers. I can write random question to console and asnwer. I have a GUIbutton. I want to; When player is clicked the button, one char will appear random. To C#, its making "Arraylist.IndexOf" and "Convert.ToInt32" but Unity 3D does not recognize. How can i fix this; (I tried to define ArrayList.IndexOf an array list. But i couldnt use it with "while"
using UnityEngine;
using System.Collections;
public class Question : MonoBehaviour {
private string questionText;
private string charLengthText;
private string totalScoreText;
private string wordText;
private int oldWordLevel;
private int newWordLevel;
string[,] kD = new string[5, 2];
byte oKD = 0;
string oK = "";
int nScore = 0;
int totalScore = 0;
int k = 0;
char[] words;
int[] randomNumber;
void Start () {
kD [0, 0] = "Answer 1";
kD [0, 1] = "Question 1";
kD [1, 0] = "Answer 2";
kD [1, 1] = "Question 2";
kD [2, 0] = "Answer 3";
kD [2, 1] = "Question 3";
kD [3, 0] = "Answer 4";
kD [3, 1] = "Question 4";
kD [4, 0] = "Answer 4";
kD [4, 1] = "Question 5";
if (newWordLevel >= 5)
return;
oKD = (byte)(Random.Range(2, 5));
questionText = " Question : " + kD[oKD, 1];
oK = kD [oKD, 0];
charLengthText = "Word char Lengt : " + oK.Length;
nScore = oK.Length * 100;
wordText = "";
for (int i = 0; i < oK.Length; i++) {
wordText += "?";
}
k = 0;
chars = new char[oK.Length];
randomNumber = new int[chars.Length];
for (int i = 0; i < randomNumber.Length; i++) {
randomNumber [i] = -1;
}
}
void OnGUI()
{
if (GUI.Button (new Rect (60, 40, 80, 20), "Button"))
chars = oK.ToCharArray ();
byte randomChar = (byte)(Random.Range(0, chars.Length));
while (ArrayList.IndexOf(randomNumber, randomChar) != -1) {
randomChar = (byte)(Random.Range (0, chars.Length));
}
randomNumber [k] = randomChar;
char takenChar = chars [randomChar];
wordText = wordText.Remove (randomChar, 1);
wordText = wordText.Insert int((randomChar), takenChar.ToString());
if (k < chars.Length - 1)
k++;
else
print ("No more char");
GUI.contentColor = Color.red;
GUI.Label(new Rect (10.0f, 0.0f, 100.0f, 75.0f), questionText);
GUI.contentColor = Color.white;
GUI.Label(new Rect (10.0f, 150.0f, 100.0f, 75.0f), wordText);
}
}
Answer by NoseKills · Sep 24, 2014 at 10:02 PM
I feel like i just probably did someone's homework but anyways...
You were missing curly brackets from your if (GUI.Button...)
and other smaller mistakes but i feel like the structure of the character revealing code was the biggest problem.
Also, it would make your code a lot more understandable if you named your variables properly. I'm not just nagging. If you want people here to help, you should make your code as easy to understand as possible. With variables called "ok" and "chars" it's not obvious what they are for.
Here's how i would do it (assuming i understood what you want)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Question : MonoBehaviour
{
private string questionText;
private string charLengthText;
private string totalScoreText;
private string wordText;
private int oldWordLevel;
private int newWordLevel;
string[,] kD = new string[5, 2];
int nScore = 0;
int totalScore = 0;
char[] chars;
ArrayList hiddenIndexes;
int[] randomNumber;
void Start () {
kD [0, 0] = "Answer 1";
kD [0, 1] = "Question 1";
kD [1, 0] = "Answer 2";
kD [1, 1] = "Question 2";
kD [2, 0] = "Answer 3";
kD [2, 1] = "Question 3";
kD [3, 0] = "Answer 4";
kD [3, 1] = "Question 4";
kD [4, 0] = "Answer 4";
kD [4, 1] = "Question 5";
if (newWordLevel >= 5)
return;
int oKD = (byte)(Random.Range(2, 5));
questionText = " Question : " + kD[oKD, 1];
wordText = kD [oKD, 0];
charLengthText = "Word char Lengt : " + wordText.Length;
nScore = wordText.Length * 100;
chars = wordText.ToCharArray();
hiddenIndexes = new ArrayList();
wordText = "";
for (int i = 0; i < chars.Length; i++)
{
wordText += "?";
// store the position of each character that is hidden (well they all are)
// this list becomes just (0,1,2,3,4...)
hiddenIndexes.Add(i);
}
}
void OnGUI()
{
if (GUI.Button (new Rect (60, 40, 80, 20), "Button"))
{
if (hiddenIndexes.Count > 0)
{
// we have a list of all hidden character positions
// let's pretend we get a 0 here
int rnd = Random.Range(0, hiddenIndexes.Count);
// check which "position" was at the random index in the list
// if rdn is 0, on the first click of the button in hiddenIndexes[0] there is a 0 as well
int charIndexToReveal = (int)hiddenIndexes[rnd];
// check the character that is in the answer at that index
char characterAtThatPosition = chars[charIndexToReveal];
// replace one of the ?'s in the answer text with the character that should be there
wordText = wordText.Remove(charIndexToReveal, 1);
wordText = wordText.Insert(charIndexToReveal, characterAtThatPosition.ToString());
// this index is no longer hidden, so we remove it from the list of hidden indexes
hiddenIndexes.RemoveAt(rnd);
// after the first pass the hiddenIndexes list is now (1,2,3,4,5...)
// on the next click, if rnd becomes 0 again, charIndexToReveal will become 1 instead this time
// and we'll end up revealing the 2nd character, and so on...
}
else
{
Debug.Log("All characters revealed");
}
}
GUI.contentColor = Color.red;
GUI.Label(new Rect (10.0f, 0.0f, 100.0f, 75.0f), questionText);
GUI.contentColor = Color.white;
GUI.Label(new Rect (10.0f, 150.0f, 100.0f, 75.0f), wordText);
}
}
Please mark the Answer accepted with the "Checkmark" button next to the thumbs. Otherwise this question will always show as unanswered.
Your answer
![](https://koobas.hobune.stream/wayback/20220613163642im_/https://answers.unity.com/themes/thub/images/avi.jpg)