- Home /
OP Resolved Issue.
GameObject list not refreshing in Update()?
Hello. So I've got a landscape made of blocks (sort of like Minecraft) and am trying to create a behavior where any block that isn't visible isn't rendered. To do this, I've started by getting every GameObject with the tag "Block" (so every block in the scene), and adding it to a list in Update(). Then I loop through the GameObjects, and I check if an upward-aimed raycast, originating from the blocks, hits anything, and if it does, then that block's Mesh Renderer is disabled. The code works at the very start of the game, but if I add a block onto an existing block in-game via Instantiate(), the block below it doesn't disappear. Why is this? Code: using UnityEngine; using System.Collections; using System.Collections.Generic; public class BlockUpdates : MonoBehaviour {
private GameObject[] objBlockList;
void Update () {
objBlockList = GameObject.FindGameObjectsWithTag("Block");
}
void LateUpdate () {
foreach(GameObject o in objBlockList)
{
MeshRenderer meshRenderer = o.GetComponent<MeshRenderer>();
Vector3 up = o.transform.TransformDirection (Vector3.up);
if(Physics.Raycast (o.transform.position, up, 5))
{
if(meshRenderer==true){
meshRenderer.enabled = false;
}
}
else
{
if(meshRenderer==false)
{
//meshRenderer.enabled = true;
}
}
}
}
}
Thanks for the help in advance.
Answer by PippyLongbeard · Dec 02, 2013 at 04:05 AM
Ah, nevermind, I figured it out. I forgot I had parented the actual object to an empty, so I was getting the MeshRenderer component from the empty in the script, not its child.
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Render a distant object 1 Answer
Trail Renderer display 1 Answer
Unity Rendering Gone Weird 1 Answer