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 ciadyuge · Sep 24, 2014 at 01:25 AM · c#array

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);
         
     }
 }
 

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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);
         
     }
 }

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 ciadyuge · Sep 25, 2014 at 10:00 AM 0
Share

You are going to heaven. Thnx alot.

avatar image NoseKills · Sep 26, 2014 at 11:13 AM 0
Share

Please mark the Answer accepted with the "Checkmark" button next to the thumbs. Otherwise this question will always show as unanswered.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Search a substring in an array of Strings 1 Answer

Inventory system using an array... 3 Answers

Writing certain values of an array into a different array 3 Answers

Javascript How to Activate/Deactivate GameObject Arrays 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