Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by AngelisDragon · Jun 10, 2013 at 08:28 AM · triggerpickupmeshrendererbattery

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 :)

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AngelisDragon · Jun 10, 2013 at 09:26 AM 0
Share

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

avatar image InfiniBuzz · Jun 10, 2013 at 09:48 AM 0
Share

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.

avatar image AngelisDragon · Jun 10, 2013 at 09:50 AM 0
Share

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 ';'".

avatar image InfiniBuzz · Jun 10, 2013 at 09:58 AM 0
Share

Have you put a semicolon ( ; ) at the end of the code lines? if no add them, if yes what is the exact error message?

avatar image AngelisDragon · Jun 10, 2013 at 09:59 AM 0
Share

Yeah I already did. Annnnd now it's working fine. Sweet. All is good :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need help with my torch/battery pickup script! 1 Answer

Flashlight pickup, battery etc 3 Answers

Destroy trigger after a certain time 2 Answers

Why does my climb stairs animation not play? 1 Answer

My movement function with jump won't work with collider function 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges