- Home /
Question by
Garrettay · Mar 15, 2020 at 12:38 AM ·
arraymeshcollidermeshrendererelement
How to delete a element from mesh rendered and mesh collider.
I want to actually delete the element not just its shader using script so it can happen on collision.
Thanks!
Comment
Answer by Link17x · Mar 15, 2020 at 12:52 AM
Get a reference to the component and edit the element you want. I'm not sure what element you mean?
// Remove the material, or you can access the array of materials
// and remove specific materials only
gameObject.GetComponent<MeshRenderer>().material = null;
// Remove the mesh object so it is invisible
gameObject.GetComponent<MeshFilter>().mesh = null;
// Remove the mesh collider material
gameObject.GetComponent<MeshCollider>().material= null;
Well what im trying to do is make it so when a tree is cut down the leafs element under materials in the mesh rendered disappears and also from the mesh collider. The tree only has two elements the trunk and the leafs. so i just need to remove the materials[1] element. public class Leafs : $$anonymous$$onoBehaviour { public GameObject leaf; // Start is called before the first frame update void Start() { //GetComponent().Stop(); }
public static void RemoveAt<T>(ref T[] arr, int index)
{
// finally, let's decrement Array's size by one
Array.Resize(ref arr, arr.Length - 1);
}
public void OnCollisionEnter(Collision collision)
{
if (collision.relativeVelocity.magnitude > 3)
{
leaf.GetComponent<ParticleSystem>().Emit(50);
var rend = GetComponent<$$anonymous$$eshRenderer>();
RemoveAt(rend, 1); // removes Color.white.
}
}
// Update is called once per frame
void Update()
{
}