How would I go about removing a mesh renderer component on collision with a trigger?

I have an automatic battery pickup, which when I walk into it, the battery is destroyed and the current battery life for the torch is extended. The only problem is, my message “Picked up battery” will not show if the trigger is gone, i.e. when the game object is destroyed. How would I go about removing just the mesh renderer component of my battery?

Here is my current script:

var batteryPower : int = 10;
        
var message : String;
var aRect : Rect;
var showMessage : boolean;
        
function OnGUI () {
        
        if (showMessage) {
                GUI.Label(aRect, message);
        }
}
        
function OnTriggerEnter () {
        
        Flashlight.lightEnergy += batteryPower; // This is to add to the current battery power
        // At the current moment I have : Destroy(gameObject); in this line, but this is what I want change to get rid of the mesh renderer component
        
        message = true;
        
        }
        
function OnTriggerStay () {
        
        message = true;
        
}
        
function OnTriggerEnter () {
        
        message = false;
        Destroy(gameObject); // And I would have the game object destroyed so as not to show the message again
        
}

Thanks :smiley:

Hi

you can use

MeshRenderer meshRenderer = GetComponent<MeshRenderer>();
meshRenderer.enabled = false;

You have two OnTriggerEnter()-Methods, in one you set message to true and in the other to false? You probably want to rename the second method to OnTriggerExit().

hope this helps.

Just curious why do you want a pick up item to not be destroyed when picked up? :slight_smile:

As a new member make sure to tick and vote the helpful answers in unity answers :slight_smile: