1
\$\begingroup\$

I've been making a simple brick-breaker game using unity 4.3 (In C#). I'm currently trying to make the blocks, which are stored in an array, all start with random colour properties. The colours are working correctly, but I want the blocks to start invisible, and then be assigned the various colours. I have researched, and found similar questions on changing the mesh-renderer component, but, as yet, they haven't worked. Here is my code:

EDIT: To put it simply: how do you make objects invisible, test if they are invisble or not and make them visble again.

using UnityEngine;
using System.Collections;

public class BlockInitialiser : MonoBehaviour {
    GameObject[] blocks;
    // Use this for initialization
    void Start () 
    {
        blocks = GameObject.FindGameObjectsWithTag ("Brick");
        StartCoroutine (blockColour ());                
    }

    IEnumerator blockColour()
    {
        foreach (GameObject block in blocks) 
                {
                        block.renderer.material.color = new Color (Random.value, Random.value, Random.value);
                        yield return new WaitForSeconds (0.1F);
                }
    }

Thanks in advance, guys.

\$\endgroup\$
1
  • \$\begingroup\$ What, exactly, isn't working? Are they not invisible? Is the color not changing? Are they all changing to the same color? \$\endgroup\$
    – House
    Apr 24 '14 at 15:42
2
\$\begingroup\$

The easiest way is to enable/disable the renderer.

Just go block.renderer.enabled = false; to turn it off, and = true; to turn it back on. You can check block.renderer.enabled to see if it's on or off.

Disabling the renderer also has the benefit of speeding up rendering.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .