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
1
Question by TheKnight · Dec 14, 2011 at 04:02 PM · texturerandomarraysswap

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.

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
2

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.

Comment
Add comment · Show 3 · 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 TheKnight · Dec 14, 2011 at 07:16 PM 0
Share

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.

avatar image Bunny83 · Dec 15, 2011 at 02:09 AM 0
Share

: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.

avatar image TheKnight · Dec 15, 2011 at 01:01 PM 0
Share

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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