How do I hide/show an object, when the player enters a trigger zone?
The guests: "Player, MeshTrigger and HiddenObject"
So the mesh trigger is working. It logs in the console when the player moves through it. (Code below!) But... how can I now make it so that when the player enters/leaves the MeshTrigger collider, an object will appear/disappear?
If it helps, the object is to be an in game "UI element" with their own trigger events.
using UnityEngine;
using System.Collections;
public class worldTrigger : MonoBehaviour {
void OnTriggerEnter (Collider other) {
if (other.CompareTag ("Player")) {
Debug.Log ("Player Has Entered");
}
}
void OnTriggerStay (Collider other) {
if (other.CompareTag ("Player")) {
Debug.Log ("...");
}
}
void OnTriggerExit (Collider other) {
if (other.CompareTag ("Player")) {
Debug.Log ("Player Has Exited");
}
}
}
Got it from here: https://unity3d.com/learn/tutorials/topics/physics/colliders-triggers
I'm sorry and thanks. :) I'm very new to C#. Please be mean so I can learn.
Answer by xitric · Oct 23, 2016 at 06:22 PM
lol nvm. Figured it out. So I named the hidden object hidey.
edit: instead of gameObject.GetComponent().enabled = false;, use gameObject.SetActive(false); to hide everything!!!
using UnityEngine;
using System.Collections;
public class worldTrigger : MonoBehaviour {
private GameObject hidey;
void Start() {
hidey = GameObject.Find("hidey");
hidey.gameObject.SetActive(false);
}
void OnTriggerEnter (Collider other) {
if (other.CompareTag ("Player")) {
hidey.gameObject.SetActive(true);
}
}
void OnTriggerExit (Collider other) {
if (other.CompareTag ("Player")) {
hidey.gameObject.SetActive(false);
}
}
}
The script disables every Tag named Player, Even the ones didnt Enter the trigger
how to make it disappear only the "Player" which Entered Trigger ??
and Also i would like to know how to reverse this script
For example Like Activating all Objects tagged "Player2" cuz i couldnt reverse it, i tried
Your answer
Follow this Question
Related Questions
How to stop a player from entering a trigger 0 Answers
OnTrigger Problems Between Colliders Issue 1 Answer
Player Can Not get off platform 1 Answer
Help on how to code crouching? 0 Answers