- Home /
Random generating answers
I've been trying to get my hands onto this, and got stuck. I know how to get random texture from an existing array
renderer.material.mainTexture = myTextures[Random.Range(0, myTextures.Length)];
but what I need is that the exact array is divided into three parts.
For example, the array is named myAnswers[], and I have three cubes. What I want is the textures to be applied on the three cubes, BUT the answers are three different answers everytime, so that none appears twice.
A little clearer, for example I have a question, 5 X 4 =, and now three answers appear : 19, 20, or 21 for example, if it's possible so that it's more realistic, if they were 8, 20, 56 it would be pretty obvious.
Somehow, the answers need to be grouped, and I think of maybe textures change and are applied on different cubes, or either they are on one cube but the cube is swapped. Oh, and the language doesn't matter.
I hope I explained well, please note if there is something unclear.
Thanks.
Answer by Bunny83 · Dec 14, 2011 at 05:14 PM
Just use a List where you can remove used items so they don't come up twice:
// C# // Your public arrays public GameObject[] myAnswers; public Texture2D[] myTextures;
// Use this to assign the textures List<Texture2D> answerTextures = new List<Texture2D> (myTextures); foreach(GameObject go in myAnswers) { if (answerTextures.Count > 0) { int textureIndex = Random.Range(0, answerTextures.Count); // Select a random texture ot of the remaining textures go.renderer.material.mainTexture = answerTextures[textureIndex]; answerTextures.RemoveAt(textureIndex); // remove this texture from the list } }
edit
The same in UnityScript ;)
// UnityScript (Javascript) // Your public arrays var myAnswers : GameObject[]; var myTextures : Texture2D[];
// Use this to assign the textures var answerTextures = new List.<Texture2D>(myTextures); for(var go : GameObject in myAnswers) { if (answerTextures.Count > 0) { var textureIndex = Random.Range(0, answerTextures.Count); // Select a random texture ot of the remaining textures go.renderer.material.mainTexture = answerTextures[textureIndex]; answerTextures.RemoveAt(textureIndex); // remove this texture from the list } }
second edit
Well, i've actually pasted the code in a script and yes, there was a typo from copy&pasting the RandomRange line ;) I've fixed it.
Also you might have to add
using System.Collections.Generic;
at the top of your C# script or
import System.Collections.Generic;
in your JS script to be able to use the generic classes.
However it should just explain how you could do it. I can't (and will not) write a whole script that fits into your project since i don't even know anything about your project except the bit of information in your question. You should try to understand it and not just C&P it and complain it doesn't work. It's your project.
Hi, and thanks for the reply!
I have troubles implementing this script in Unity. As far as I know, this should be a C# script, shouldn't it? Don't know if there is a foreach in unityscript, maybe I'm awfully wrong, but the declaration of GameObject confuses me, cause I don't think that it's assigned like that in C#. Is this supposed to be a guidance - kind of pseudo-code? Sorry for the dumb question, confusion is caused by the recent change of the languages.
Also, why can't I upvote anyone?
Thanks.
:D You said the language doesn't matter which is a good attitude, but i thought you could convert between the two (or three?) languages ;).
Anyway i've added the same in UnityScript.
If I leave the code as you gave, it gives lots of errors. If I put the second part of the script in the Start functions, still gives other errors.
What am I supposed to fill at the "GameObject go in myAnswers" part in the braces?
Your answer
Follow this Question
Related Questions
Writing to GUI from an array 0 Answers
question about arrays 1 Answer
GUI.Button Texture Swap? 3 Answers
Beginner : Random gameobject from array 2 Answers
Random place changement 2 Answers