- Home /
How do I make all game objects with a certain tag appear on trigger enter? (C#)
I'm a new Unity user.
To make my scene perform better, I have switched the mesh renderer off for objects that don't necessarily need to be seen.
However, when the player enters a collider, I want those objects (assigned the tag "Appear") to have their meshes rendered so that they may become visible.
using UnityEngine;
using System.Collections;
public class RenderTrig : MonoBehaviour {
void OnTriggerEnter(Collider other) {
GameObject Appear = GameObject.Find("Appear");
(Appear).GetComponent<MeshRenderer>().enabled=true;
}
}
I tried to create what I thought would do the trick, called the script RenderTrig, and placed it on my collider, which I named TRIGGIFER. I know, however, that I'm doing something fatally wrong, but I don't know where my mistake lies. What do I need to do?
Thanks for your help in advance!
If you have other dynamic gameobjects in your game that might spring this trigger, you should put an "If(other.tag == "Player") in your OnTriggerEnter to ensure that "other" is indeed the player, what if it's a random chicken clucking by or anything.
Also add a debug line immediately after OnTriggerEnter like 'Debug.Log("Trigger sprung by: " + other.name); so you can ensure you are getting the trigger event you need.
Please do not post a comment or follow-up as an Answer. I converted that one for you but normally I just reject out of the mod queue cuz otherwise mods would do nothing but convert all day long.
Anyway, to your question - I suspect the little snippets I posted would be language agnostic but my intent was C# as that's what I use.
Assu$$anonymous$$g your trigger does work.
You should be using this : FindGameObjectsWithTag
Answer by erfan · Sep 05, 2015 at 07:52 AM
You told that "Tag" not "Name". \n;
GameObject.Find("GameObjectName");
Will Find An Object That It's name is "GameObjectName" You can use this.
GameObject.FindGameObjectWithTag("MyTag");
Answer by jmsearing · Sep 09, 2015 at 12:43 PM
Get an array of all objects with the tag "Appear". For each item in the array, set the mesh renderer to enabled = true.
public class NameOfYourTriggersClass : MonoBehaviour {
public GameObject[] visibleList;
void OnTriggerEnter(Collider other) {
//here's the script you need to create an array of gameobjects with tag: Appear
if (visibleList == null) {
visibleList = GameObject.FindGameObjectsWithTag("Appear");
}
//set each object's Mesh Rendere to enabled that appears in the list
foreach (GameObject visible in visibleList) {
visible.GetComponent<MeshRenderer>().enabled=true;
}
}
Haven't tested that but it should work as long as the trigger is actually triggering. Might get some errors or warning for not using the Collider of the triggering object.
Your answer
![](https://koobas.hobune.stream/wayback/20220612002659im_/https://answers.unity.com/themes/thub/images/avi.jpg)