K.J. Shamberger
K.J. Shamberger

Reputation: 69

Apply materials to every other object in Unity using code

I'm trying to spawn 8 quads in a row and then assign a material to every other quad, so it would look like a checkerboard pattern. The code I have accomplishes that, but I was wondering if there was a way to simplify it. So if I were to create more quads in different positions (for example, I wanted to create a row above the one I've already created), I wouldn't have a long list of repetitive code.

    for (i = 0; i < 8; i++)
    {
        GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
        quad.transform.position = new Vector3(i * 1, 0, 0);

        if (i == 1)
        {
            MeshRenderer mesh = quad.GetComponent<MeshRenderer>();
            mesh.material = (Material)Resources.Load("Charcoal");
        }

        if (i == 3)
        {
            MeshRenderer mesh = quad.GetComponent<MeshRenderer>();
            mesh.material = (Material)Resources.Load("Charcoal");
        }

        if (i == 5)
        {
            MeshRenderer mesh = quad.GetComponent<MeshRenderer>();
            mesh.material = (Material)Resources.Load("Charcoal");
        }

        if (i == 7)
        {
            MeshRenderer mesh = quad.GetComponent<MeshRenderer>();
            mesh.material = (Material)Resources.Load("Charcoal");
        }

        else
        {
            MeshRenderer mesh = quad.GetComponent<MeshRenderer>();
            mesh.material = (Material)Resources.Load("Bone");
        }

    }

My code checks to see if the position of the quad is equal to 1, 3, 5 or 7, and applies my material, then it uses another material of mine to fill the quads that aren't in those positions.

Upvotes: 0

Views: 465

Answers (2)

Arslan Ali
Arslan Ali

Reputation: 460

Create Gamobject Prefab Quad and add empty gameobject in scene with adding script board Now assign this Quad prefab to board script public gameobject then run it in unity and it will generate the checker board as well considering even/odd logic

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class board : MonoBehaviour {

// Use this for initialization
public GameObject quad;
void Start () {
    for(int j=-4;j<4;j++)
    {
        for(int i=-4;i<4;i++)
        {
            var gameObject=Instantiate(quad,new Vector3(i,j,0),Quaternion.identity);

            if (j % 2 == 0)
            {
                if (i % 2 == 0)
                    gameObject.GetComponent<MeshRenderer>().material = (Material)Resources.Load("bone");
                else
                    gameObject.GetComponent<MeshRenderer>().material = (Material)Resources.Load("Charcoal");
            }
            else
            {
                if (i % 2 != 0)
                    gameObject.GetComponent<MeshRenderer>().material = (Material)Resources.Load("bone");
                else
                    gameObject.GetComponent<MeshRenderer>().material = (Material)Resources.Load("Charcoal");
            }
        }
    }
}

// Update is called once per frame
void Update () {

}
}

Resulting scene assign prefab as enter image description here

Output Result checker

enter image description here

Upvotes: 2

Vinicius Victor
Vinicius Victor

Reputation: 358

Simply check if i is odd. If it is then paint it with charcoal. Something like this:

for (i = 0; i < 8; i++)
{
    GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
    quad.transform.position = new Vector3(i * 1, 0, 0);
    MeshRenderer mesh = quad.GetComponent<MeshRenderer>();        
    if (i%2 != 0) // if i is odd
    {
        mesh.material = (Material)Resources.Load("Charcoal");
    } else // if i is even
    {
        mesh.material = (Material)Resources.Load("Bone");
    }
}

Upvotes: 2

Related Questions