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
0
Question by WALK2222 · Apr 21, 2013 at 07:55 PM · gameobjectrenderingrenderinvisible

Making a game object un-render or turn-invisible through trigger?

I'm trying to make this separate game object disappear when the player passes through a trigger. I know how to do this when I am referencing the same game object that the script is contained in, but how would I do this for a game object that is located somewhere different than where the script is?

For example: The player walks through a trigger, and a box in the far corner of the room turns invisible.

Thanks!

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 Iceblitzyt · Apr 21, 2013 at 08:02 PM

Okay, I'm going to give you a simple script, I just quickly wrote this i hope it's what you mean, In the example I placed this script to a cube, i had a player and a red cube. When the player moved close enough to the white cube the red one disappeared.

 using UnityEngine;
 using System.Collections;
 
 public class DestroyRedCube : MonoBehaviour {
 public GameObject Player;
 
 void Update(){
 
   float PlayerTrigger = new Vector3(Player.position, transform.position);
 
 If( PlayerTrigger < 5){
 GetComponent<Mesh Renderer>().enabled = false;
 }
 }
 }

Please note I wrote that freehand on this reply section I did not load up unity to check if it works, Some of the text which is in uppercase may need lowercase letters, Unity will tell you if you need to change it. If the problem is persistent, I'll open up unity and check myself.

EDIT - This was written after replying to comments:

Well all you need to do is setup the player trigger like in did in the script above and add a second game object IE Monster. So it will be:

 Public GameObject Monster

Followed with a tweak in the update : Monster.GetComponent().enabled = false;

If you have other scripts attached to the monster, IE AI health ect ect, copy and pase the code above under that, but replace the MeshRenderer with whatever script is active on the monster. IE Or .

I just tested this in unity using a simple press Q to disable a cube:

 using UnityEngine;
 using System.Collections;
 
 public class Tester : MonoBehaviour {
     public GameObject Redcube;
     void Update () {
     if(Input.GetKeyDown(KeyCode.Q)){
             Redcube.GetComponent<MeshRenderer>().enabled = false;
         }
         
         
     }
 }


It works 100% in the same principle, just attach the script to your trigger, It can be an empty game object or door, In the inspector add the player and monster components. Then set whatever distance from the door you want, it could be re worked from my original answer to:

 using UnityEngine;
 using System.Collections;
  
 public class DestroyRedCube : MonoBehaviour {
 public GameObject Player;
 public GameObject Monster;
  
 void Update(){
  
   float PlayerTrigger = Vector3.Distance(Player.position, transform.position);
  
     if( PlayerTrigger == 0){
     Monster.GetComponent<MeshRenderer>().enabled = false;
 }
 }
 }

Hope this works for you now.

Comment
Add comment · Show 2 · 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 WALK2222 · Apr 21, 2013 at 09:52 PM 0
Share

Sorry that didn't work :c I'll try to explain this better

I have a monster, and I have a trigger. I want the monster to disappear when the player walks through the trigger.

avatar image Iceblitzyt · Apr 21, 2013 at 10:22 PM 0
Share

Okay, I see the problem slight uppercase errors and missing a .distance... I'll edit the issue, should work 100% now.

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

13 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

Related Questions

Unity renders scene weird 1 Answer

How do I make all game objects with a certain tag appear on trigger enter? (C#) 2 Answers

Imported models partially disappear 0 Answers

A node in a childnode? 1 Answer

Determining How Many Objects are Rendered by the Main Camera 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