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 Skatola · Feb 03, 2013 at 06:16 PM · arraymaterialsrandomize

array with a list of materials

hi :) i need to build one array with a list of materials and assign one of them randomly to one gameobject, but i dunno the code for make an array of materials, any suggestion would be really appreciated, i'm just a newbye. And sry for my english, i understand it but i suck to write in eng

Comment
Add comment · Show 2
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 Wolfram · Feb 03, 2013 at 06:30 PM 0
Share

No worries, you are definitely in the "Top-10% of understandable language and questions" on this site. ;-)

avatar image Wolfram · Feb 03, 2013 at 06:34 PM 0
Share

Where do these $$anonymous$$aterials come from, are they randomly created themselves?

I'd give you a C# example, but as most people starting with Unity use UnityScript, it might be better to wait for an example in UnityScript. Or find an example/tutorial about that on this site, on the Unify wiki, on a JavaScript site, or on Google. There must be plenty around.

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by T27M · Feb 03, 2013 at 06:39 PM

Here's one in C# that will set a material at start up, I would think lists would be better?

 using UnityEngine;
 using System.Collections;
 
 
 public class arrayDemo : MonoBehaviour
 {
     System.Random random = new System.Random();
     
     // Set the materials in the inspector
     public Material[] myMaterials = new Material[5];
 
     // Use this for initialization
     void Start ()
     {
         // Assigns a random material at start
         gameObject.renderer.material = myMaterials[random.Next(0,myMaterials.Length)];
     }
 }

Here's a one written in Unityscript with some possible uses

 #pragma strict
 
 // If you have a set amount of materials using arrays will be fine, but if you need to resize on the fly use lists
 // Dont forget to assign the materials in the inspector
 public var myMaterials : Material[];
 var random = new System.Random();
 
 // Using lists
 public var myMaterialList = new System.Collections.Generic.List.<Material>();
 
 function Start ()
 {
     ChooseRandomMaterial ()
 }
 
 // Could call this function from somewhere else to change the material
 function ChooseRandomMaterial ()
 {
     //Array
     gameObject.renderer.material = myMaterials[random.Next(0,myMaterials.Length)];
     
     //List
     gameObject.renderer.material = myMaterialList.Item[random.Next(0,myMaterialList.Count)];
 }
 
 //Could call this function from somewhere else to remove a material from the list
 function RemoveMaterial()
 {
     //List only
 
     // Remove random material
     myMaterialList.RemoveAt(random.Next(0,myMaterialList.Count));
 }
 
 function RemoveMaterial( removeMe : Material)
 {
     //List only
     
     // Remove by name
     myMaterialList.Remove(removeMe);
 }
 
 //Could call this function from somewhere else to and pass a new material into the list. 
 function AddNewMaterial( myNewMaterial : Material )
 {
     //List only
     myMaterialList.Add(myNewMaterial);
 }

Try out both so you know how to use them, but choose one or the other for your project.

Accessing arrays

Comment
Add comment · Show 7 · 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 Skatola · Feb 03, 2013 at 08:33 PM 0
Share

excuse me i forgot to say i use javascript only, can u write it in js? thank you so much : )

avatar image T27M · Feb 03, 2013 at 11:05 PM 0
Share

public var my$$anonymous$$aterialList = new System.Collections.Generic.List.();

Is this me doing it wrong? The code box changed this line.

avatar image Skatola · Feb 04, 2013 at 12:09 AM 0
Share

thank you so much!

avatar image T27M · Feb 04, 2013 at 12:24 AM 0
Share

If you get an error (which I did) if I didn't add this ending

 List.<$$anonymous$$aterial>"();

To the line I mentioned above. The code formatting removed it.

avatar image Skatola · Feb 04, 2013 at 03:34 AM 0
Share

T27$$anonymous$$ i tried your suggestions and i choose to use a List. I change my $$anonymous$$d and i use it for a gameobjects list and not for materials, i did it in this way:

public var GOSlist = new System.Collections.Generic.List.();

like u said i've assigned the gameobjects in the inspector, now i can succesfully instantiate randomly one of the gameobjects in the list with:

Instantiate (GOSlist.Item[Random.Range(0,GOSlist.Count)]);

but how can i remove the same gameobject randomly generated from the list? Removeme doesn't work or i use it incorrectly :(

Show more comments
avatar image
1

Answer by cdrandin · Feb 03, 2013 at 06:33 PM

 //Define a list using Javascript
 var myList = new List.<int>(); 
 var anotherList = new List.<SomeClass>();
  
 //Define a list using C#
 List<int> myList = new List<int>();
 List<SomeClass> anotherList = new List<SomeClass>();

Brought to you by UnityGems

Comment
Add comment · Show 1 · 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 Wolfram · Feb 03, 2013 at 06:37 PM 0
Share

Very good link!

Note that Unity Answers eats up any code within lessThan/greatherThan symbols, so @skatola, just follow the link and look at the section "How to store lists of objects that can grow".

avatar image
0

Answer by yashaswi · Feb 09, 2014 at 07:52 AM

// Using lists alt text instead use this... Add namespace: using System.Collections.Generic;

and in the class declare like this alt text


header.jpg (9.5 kB)
material.jpg (11.2 kB)
Comment
Add comment · 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

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

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Missing Materials on build 1 Answer

type ...Material[] vs Material (materials change at run time) 1 Answer

How to replace materials in the materials array 3 Answers

Question (or recommendation) on my data structure for getting Prefabs into an array (as gameobjects?) 0 Answers

Cache materials into List (or array) and restore from cached List? 0 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