The purpose is to put the red prefaps instead of the white prefeps removed.
But since I use this code (Instantiate(two,hit.point,Quaternion.identity)), where does the mouse click creating a new one there.
How do I replace white prefaps with red prefabs? (Same coordinates)
for (int x = 0; x < width; x++)
{for (int z = 0; z < height; z++){
GameObject gridPlane = (GameObject)Instantiate(one);
gridPlane.transform.position = new Vector3(gridPlane.transform.position.x + x,
gridPlane.transform.position.y, gridPlane.transform.position.z + z);
grid[x,z] = gridPlane;}}
and,
if(Input.GetMouseButtonDown(0)){
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
Destroy (hit.transform.gameObject);
Instantiate(two,hit.point,Quaternion.identity);}}
You can just Instantiate the red prefab before destroying the white.
if(Input.GetMouseButtonDown(0)){
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
Instantiate(two,hit.transform.position,hit.transform.rotation);}}
Destroy (hit.transform.gameObject);
}
}
Edit:
Though I suggest using color change which could give better performance as @PizzaPie suggested.
You can count the total number of white box and red box by using a manager script attached to an empty gameObject.
So an example of what the manager script should have.
public int whiteCount = 0;
public int redCount = 0;
and within your whitebox or redbox script call
whiteCount += 1;
redCount -= 1;
when you are changing the colors.
To change the material colors, given that you only need two materials, you don’t need a list.
Just add the following to your gameobject script
public Material redMaterial;
public Material whiteMaterial;
and when you know that you want to change the material