- Home /
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!
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.
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.
Okay, I see the problem slight uppercase errors and missing a .distance... I'll edit the issue, should work 100% now.