- Home /
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 :D
Answer by InfiniBuzz · Jun 10, 2013 at 09:13 AM
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? :)
As a new member make sure to tick and vote the helpful answers in unity answers :)
Sweet thanks :)
Oh and I have OnTriggerExit() on my project, but I can't copy and paste from it because it isn't connected to the internet :/
As for your other question, I have the collider around my battery object, so when you walk into the collider it displays the message and adds the battery life. If I don't get the object to disappear, it seems strange that the battery stays for a second or two before you move on. Is there a more efficient way to do this?
Thanks again :D
This is because you destroy the object in the OnTriggerExit method so as long as your character stays on the item it wont disappear. I now realized that I didn't think too much before typing that question ;)
You could handle the message displaying seperately but its not needed if its working like you want it.
Ok I need help again. I put this into my script at line 17 (was this right?) and now errors are co$$anonymous$$g up, such as "Please insert a ';'".
Have you put a semicolon ( ; ) at the end of the code lines? if no add them, if yes what is the exact error message?
Yeah I already did. Annnnd now it's working fine. Sweet. All is good :)