(1)スクリプトの作成①
- 生成するオブジェクトに「特定の色」を追加したい場合
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxGene : MonoBehaviour
{
public GameObject boxPrefab;
private void Start()
{
// 反復(繰り返し文)
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
GameObject box = Instantiate(boxPrefab, new Vector3(i * 1.2f, 0.5f, j * 1.2f), Quaternion.identity);
box.GetComponent<MeshRenderer>().material.color = Color.blue;
}
}
}
}

(2)スクリプトの作成②
- 生成するオブジェクトにランダムに色を追加したい場合
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BoxGene : MonoBehaviour
{
public GameObject boxPrefab;
private void Start()
{
// 反復(繰り返し文)
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
GameObject box = Instantiate(boxPrefab, new Vector3(i * 1.2f, 0.5f, j * 1.2f), Quaternion.identity);
box.GetComponent<MeshRenderer>().material.color = new Color(Random.value, Random.value, Random.value, 1.0f);
}
}
}
}
