How do I change the MATERIAL of this mesh when i press T
So I have this
using UnityEngine; using System.Collections;
// Change renderer's material each changeInterval // seconds from the material array defined in the inspector. public class ExampleClass : MonoBehaviour { public Material[] materials; public float changeInterval = 0.33F; public Renderer rend;
void Start() {
rend = GetComponent<Renderer>();
rend.enabled = true;
}
void Update() {
if (materials.Length == 0)
return;
// we want this material index now
int index = Mathf.FloorToInt(Time.time / changeInterval);
// take a modulo with materials count so that animation repeats
index = index % materials.Length;
// assign it to the renderer
rend.sharedMaterial = materials[index];
}
} and it does what i want but it is on a timer so i tried to edit it but it didn't work I am horrible at C# and almost slightly decent at Javascript . I want it to switch between Materials NOT textures i need them to be materials drag and drop in the inspector and switch through them when i press "T" or "t" you know what I would also be happy with E R Y U I O P L K J H G V B N M any ideas .
Answer by itsharshdeep · Aug 04, 2016 at 12:39 PM
public Material[] materials;
public float changeInterval = 0.33F;
public Renderer rend;
public Material additionalSingleMaterial;
void Start() {
rend = GetComponent<Renderer>();
rend.enabled = true;
}
void Update() {
// Untill T is pressed it will change/swtich b/w multiple materials :
if (Input.GetKey (KeyCode.T)) {
if (materials.Length == 0)
return;
// we want this material index now
int index = Mathf.FloorToInt (Time.time / changeInterval);
// take a modulo with materials count so that animation repeats
index = index % materials.Length;
// assign it to the renderer
rend.sharedMaterial = materials [index];
}
// if you want to chage material only once then following code
if (Input.GetKey (KeyCode.P)) {
rend.sharedMaterial = additionalSingleMaterial;
}
}
Attach script may help you in what you want to achieve. PLease let me know if you need any thing else in script. JavaScript version:
#pragma strict
var materials : Material[];
var changeInterval : float;
var rend : Renderer;
var additionalSingleMaterial : Material;
var index: int;
function Start () {
rend = gameObject.GetComponent(Renderer);
rend.enabled = true;
}
function Update () {
// Untill T is pressed it will change/swtich b/w multiple materials :
if (Input.GetKey (KeyCode.T)) {
if (materials.Length == 0){
return;
}
// we want this material index now
index = Mathf.FloorToInt (Time.time / changeInterval);
// take a modulo with materials count so that animation repeats
index = index % materials.Length;
// assign it to the renderer
rend.sharedMaterial = materials [index];
}
// if you want to chage material only once then following code
if (Input.GetKey (KeyCode.P)) {
rend.sharedMaterial = additionalSingleMaterial;
}
}
Answer by Zastion · Aug 04, 2016 at 10:18 PM
Okay thanks for the help ! exactly what i wanted ! but one question why does it go through the index randomly?(not a complaint) @ itsharshdeep
mm... First of all next time you need to post this response as a comment on the respective answer ins$$anonymous$$d of the adding a new answer.. because this is response to any answer not a answer..
I don't think so that it is going random.. I checked by putting log.. It is already in sequence ...