- Home /
Can you use renderer.enabled on array members ?
Hello I am a beginner so I'm not sure if this is possible.
I am instantiating a column of cubes and I am trying to show / hide each member based on a value.
Each member of the array is the same prefab.
In this code all 5 blocks are showing, even though I only have renderer.enabled true on the second member.
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class PlantStats : MonoBehaviour {
public GameObject[] moisHealth; public GameObject[] tempHealth; public GameObject[] lightHealth;
private float xDirection = -0.24f,
moisYDirection = 0.24f,
tempYDirection = 0.24f,
lightYDirection = 0.24f,
zDirection = -0.07f,
moisGrowAmount = 0.09f,
tempGrowAmount = 0.09f;
private int moisHealthChecker = 0,
moisArrayValue = 0,
tempArrayValue = 0,
lightArrayValue = 0,
plantHealth = 0,
// sensor values moisSensorValue = 0;
void Start ()
{
PopulateMoisture();
PopulateTemp();
PopulateLight();
}
void Update ()
{
if (Input.GetKey(KeyCode.Alpha1))
{
moisHealth[0].renderer.enabled = true;
}
}
void PopulateMoisture()
{
for (int i = 0; i<=4; i++)
{
Vector3 cubeMoisPosition = new Vector3(transform.position.x + xDirection, transform.position.y + moisYDirection, transform.position.z + zDirection);
GameObject newMoisture = Instantiate(moisHealth[moisArrayValue], cubeMoisPosition, transform.rotation) as GameObject;
moisYDirection = moisYDirection + moisGrowAmount;
moisArrayValue++;
}
moisHealth[0].renderer.enabled = false;
moisHealth[1].renderer.enabled = true;
}
Answer by DaveA · Aug 12, 2013 at 09:39 PM
You have coded it. Have you tried it? If the question is, can I do this kind of thing:
moisHealth[0].renderer.enabled = false;
yeah, sure.
Answer by TimBorquez · Aug 13, 2013 at 12:33 AM
yup if your array members have a renderer component you can access it
Answer by mcarney82 · Aug 13, 2013 at 11:59 AM
OK thanks a lot guys. Now that I know I can...
I'll have to see why it's not working in my above code.
I am currently saying
moisHealth[0].renderer.enabled = false; moisHealth[1].renderer.enabled = true;
but moiseHealth[0] IS rendering.