using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SquadsGenerator : MonoBehaviour
{
public GameObject squadPrefab;
public int numberOfSquads;
public int numberOfMembersInsquad;
private GameObject squadsParent;
private void Start()
{
squadsParent = GameObject.Find("Squads");
GenerateSquads(numberOfSquads, numberOfMembersInsquad, squadPrefab);
}
// Update is called once per frame
void Update()
{
}
public void GenerateSquads(int squadsCount,
int numberOfMembers,
GameObject squadMemberPrefab)
{
for (int i = 0; i < squadsCount; i++)
{
GameObject newSquad = new GameObject();
newSquad.name = "Squad " + i;
newSquad.tag = "Squad";
newSquad.transform.parent = squadsParent.transform;
ColorSquads(newSquad);
for (int x = 0; x < numberOfMembers; x++)
{
var go = Instantiate(squadMemberPrefab);
go.name = "Member " + x;
go.tag = "Squad Member";
go.transform.parent = newSquad.transform;
}
}
}
private void ColorSquads(GameObject squad)
{
//Fetch the Renderer from the GameObject
Renderer rend = squad.GetComponent<Renderer>();
//Set the main Color of the Material to green
rend.material.shader = Shader.Find("_Color");
rend.material.SetColor("_Color", Color.green);
//Find the Specular shader and change its Color to red
rend.material.shader = Shader.Find("Specular");
rend.material.SetColor("_SpecColor", Color.red);
}
}
But now it will color all the squads in green.
I want that it will color the first squad for example in green the next squad in red the next in green and so on. green,red,green,red....
To extend the method ColorSquads to get two colors for example Color 1, Color 2 and this will set the colors switch to color each squad once in Color 1 once in Color 2.
Screenshot of the hierarchy each Squad is empty gameobject and the squad members are child of each squad.