- Home /
Question by
david-gott · Apr 04, 2020 at 08:55 PM ·
c#materialmaterialsmeshrenderer
Change specific material on an object with multiple materials?,Change specific material on an object in a script?
I have an object with a meshrenderer with 5 materials on it, and I just want to change one, when I press z for example. This is the code I have, however, it changes the "0th" material, instead of the 3rd material, which I want to do, but I'm not sure how to do that.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class flashingLight : MonoBehaviour
{
public Material Material1;
public GameObject Object;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("z"))
{
Object.GetComponent<MeshRenderer>().material = Material1;
}
}
},I have an object with a meshrenderer with 5 materials on it, and I just want to change one, when I press z for example. This is the code I have, however, it changes the first material, instead of the 4th material, which I want to do, but I'm not sure how to do that.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class flashingLight : MonoBehaviour
{
public Material Material1;
public GameObject Object;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("z"))
{
Object.GetComponent<MeshRenderer>().material = Material1;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Is it bad to have this many materials? 2 Answers
How to make a cube with metallic look and soft edges [pic included] 1 Answer