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!
erfan
2
You told that “Tag” not “Name”.
;
GameObject.Find("GameObjectName");
Will Find An Object That It’s name is “GameObjectName”
You can use this.
GameObject.FindGameObjectWithTag("MyTag");
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.