HI, and Thank you for your help. I made a Maze Generator, and I want to assign a material to all of the walls in the maze. I am doing this by creating a list with all of the walls in the game, Colors. I achieve this by finding all game objects with the name Wall(Clone), because they are all instantiated. I Then Add a script which changes the material to my desired material (in FloorColor). I then change the name of the wall from “WallClone” to “TempWall” to show that the wall has been affected. I then set the Temp wall Game object to Null, and repeat the scenario until the number of walls in Color is = to the total walls in the game.
void ColorWalls()
{
Debug.Log(totalwallcolor);
while (Colors.Count < totalwallcolor)
{
GameObject Tempwall = GameObject.Find("Wall(Clone)");
Tempwall.AddComponent<FloorColor>();
Tempwall.name = ("tempwall");
Tempwall = null;
if(Colors.Count == totalwallcolor)
{
Debug.Log(Colors.Count + "amount in colors");
}
Colors.Add(Tempwall);
}
}
My issue is that the Code Debugs the correct amount of walls in the Color list, Color = Totalwalls, but in the game Heiarchy about 40% of my walls are still WallClone and without the correct script! What Am I doing Wrong? Also, the number of walls not affected changes every time.
You should probably try doing something like public GameObject objects; (this is an array) or public List objects (this one I have less experience with so I don’t know too much of the syntax). Then, unfortunately you would have to add in the walls manually into the array, but you could also do it in the inspector. Then, in the update function, do a for loop which will go through each of the different walls, noting that you will have to make it loop through until one less then the final wall. This is because with arrays, the first item in the array is index[0]. Then, inside of the for loop, you put the code you had before, except you take out the Color.Add(Tempwall);, because this will add walls to the array of walls that you already have. It should look something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScriptForUnityAnswers : MonoBehaviour {
public GameObject[] objects; //Here you go into the inspector and add in the walls manually
void Start () {
}
void Update ()
{
ColorWalls ();
}
void ColorWalls () {
int i;
for (i = 0; i < objects *; i++)*
-
{*
-
GameObject Tempwall = GameObject.Find ("Wall(Clone)");*
-
Tempwall.AddComponent<FloorColor> ();*
-
Tempwall.name = ("tempwall");*
-
Tempwall = null;*
-
if (Colors.Count == totalwallcolor)*
-
{*
-
Debug.Log (Colors.Count + "amount in colors");*
-
}*
-
}*
- }*
- //Don’t forget your Colors void*
}
You can use a List<T>
, being T the object type you want, can be GameObject or Wall or whatever.
List is dynamic so you can do something like this:
List<GameObject> myList = new List<GameObject>();
void ColorWalls(){
var maxQuantity = 10;
Debug.Log("Start Finding Walls!");
for (int i = 0; i < maxQuantity; i++)
{
GameObject Tempwall = GameObject.Find("Wall(Clone)");
Tempwall.AddComponent<FloorColor>();
Tempwall.name = ("tempwall");
myList.Add(Tempwall);
}
Debug.Log("Finished!");
}
However calling GameObject.Find("Wall(Clone)"
will not warranty that will find every Wall(Clone) in the scene. Perhaps it repeats some of them.
You can tag gameObjects and find all gameObjects with the desire tag.