- Home /
How to apply different materials to spawned chracters?
I use a script for spawning characters and I want each character to use a different material, so all of they look different. Materials are stored as a jpg files in my projects. What are the ways to make this work?
IEnumerator InstanceSpawner()
{
yield return new WaitForSeconds(0);
while (!stop)
{
RandomHuman = Random.Range(0, 2);
Vector3 SpawnPosition = new Vector3(Random.Range(-SpawnValues.x, SpawnValues.x), 1, Random.Range(-SpawnValues.z, SpawnValues.z));
Instantiate(humans[RandomHuman], SpawnPosition + transform.TransformPoint(0,0,0), Quaternion.Euler(0,90,0));
yield return new WaitForSeconds(SpawnWait);
}
}
}
Answer by Monsoonexe · Mar 08 at 04:28 AM
Instantiate returns a reference to the new object.GameObject newHuman = Instantiate(....); Then, you can use GetComponent() to get a component you want. Often, you're using a MeshRenderer component to color the object. So,
MeshRenderer meshRenderer = newHuman.GetComponent<MeshRenderer>();
Then get at its color.
meshRenderer.material.color = Color.Blue;
Your answer
Follow this Question
Related Questions
Spawning help 1 Answer
Spawning prefabs dependant upon Health UI? 2 Answers
Advanced Wave Spawner. 1 Answer
Most efficient way to create/spawn a lot of enemies at the same time(Unity3D) 1 Answer
Respawn script not working! 0 Answers