C# gameobject find and disable

Hello,

I have some code written in C#, which starts and stops a particle system when entering a trigger. What I have an issue is finding a gameobject in code with C# to disable it’s mesh renderer. I know how to do this in Javascript. Can anyone point out in my “OnTriggerExit” what should be the correct syntax.

using UnityEngine;
using System.Collections;

public class Mine_Explode : MonoBehaviour 
{
   public AudioClip Hurt;
   private GameObject Mine;
	
	void Start () 
	{
    	particleSystem.Stop();
	}
     
    void OnTriggerEnter(Collider other) 
	{
    if (other.tag == "Weapon") 
		{
    		particleSystem.Play();
			audio.PlayOneShot(Hurt);
    	}
   	}
     
    void OnTriggerExit(Collider other) 
	{
    if (other.tag == "Weapon") 
		{
    		Destroy(particleSystem);
			Mine = GameObject.Find("Gas canister");
			Mine.GetComponent<MeshRenderer>.enabled = false;	
    	}
    }
}

Cheers!

1 Answer

1

Mine.GetComponent().enabled = false;

Just missing some ‘(’, ‘)’

Perfect! I hate it when I miss something so simple. Thanks for your time!

Hi when doing this, object get invisible right. insted of that can able to apply another material.

Sure you can just check out the docs of the mesh renderer and change the mat. You may just need to get the component of the material