- Home /
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
No worries, you are definitely in the "Top-10% of understandable language and questions" on this site. ;-)
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.
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.
excuse me i forgot to say i use javascript only, can u write it in js? thank you so much : )
public var my$$anonymous$$aterialList = new System.Collections.Generic.List.();
Is this me doing it wrong? The code box changed this line.
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.
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 :(
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
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".
Answer by yashaswi · Feb 09, 2014 at 07:52 AM
// Using lists instead use this... Add namespace: using System.Collections.Generic;
and in the class declare like this