Sorry for asking as I am new to Unity3D. ![]()
I would like to add some spheres then changing their textures (material?) So I wonder how this is done programmatically? Would you please shed some light on this by giving code examples.
Sorry for asking as I am new to Unity3D. ![]()
I would like to add some spheres then changing their textures (material?) So I wonder how this is done programmatically? Would you please shed some light on this by giving code examples.
Well, if you have some prefabs ready to be used, you could just have a little prefab array that you would instantiate according to an index position:
using UnityEngine;
using System.Collections;
public class Generator : MonoBehaviour {
public GameObject[] prefab;
private GameObject go;
private int index;
public void destroyObject()
{
Destroy(go);
}
/* Creates a new instance for your prefab. */
public void instance()
{
go = (GameObject) Instantiate(prefab[index], transform.position, transform.rotation);
}
}
Of course you’d need to create a game object in Unity and attach this script to it. After that, you’d need to assign some prefabs to the prefabs array in order for this to work.
As for changing the textures/material, you can do both. For materials:
this.gameObject.renderer.material.mainTexture = mats[index].mainTexture;
or for textures:
this.gameObject.renderer.material = mats[index];
where mats[ ] would be a Materials array. Code may vary slightly (and you need to add more code in order for this to work), but this could get you started.
Good luck!
EDIT: Sorry, I forgot to edit some function and variable names. I took this from some code of mine where variable and function names are in Spanish. Should be fine now. ![]()
Ok, thank so much.