How do I replace white prefaps with red prefabs?
Hi, I want to do something like below.
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);}}
I'm printing the total number of boxes.I can see how long you have stayed when the number of boxes is getting low. (red or white box) So I wanted to do it using a prefaps.If the color changes, can I say I have one in this color? For example, white box total count = 2 red box total count = 0
When I click on the box, white box total count = 1 (-1) red box total count = 1 (+1)
You could use an enum or bool that represents current state and you change it when you change the actual color.After that create a class tha controls the counts and with a reference to it in the box class add or subtract accordingly when changes occure. From performance aspect that is better than constant instatiating and destorying.
Answer by KKS21199 · Apr 06, 2017 at 05:35 PM
Hey,
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
hit.transform.gameObject.GetComponent<Renderer>().material = redMaterial;
hit.transform.position,hit.transform.rotation hit.transform.gameObject.GetComponent().material = red$$anonymous$$aterial;
The first code is doing the work I want to do.So In the second code, the color change succeeded. Problem, the counts as +1 when clicked with the mouse again on the red square. To prevent this, I added a bool.But just clicking with the mouse does not allow you to click on the entire field, not the square.
But, @PizzaPie and as you said, I will not compare two methods in terms of performance.
I'll work on it a little more.
Accept the answer if it works. You can do a simple if check to make sure the count doesnt count if double clicking on a red tile.
if( hit.transform.gameObject.GetComponent<Renderer>().material == white$$anonymous$$aterial)
{
redcount ++;
whitecount --;
hit.transform.gameObject.GetComponent<Renderer>().material = red$$anonymous$$aterial;
}
just make sure to set the white$$anonymous$$aterial when instantiating each of the white prefab so the reference is set right.
Your answer
![](https://koobas.hobune.stream/wayback/20220612114831im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to handle levels 0 Answers
Unity Addressable for player skins 0 Answers
I have searched like the whole Internet for this lol (get Value of specific Prefab(clone)) 2 Answers
connect 3 gameobjects 0 Answers
How can I update the gameobjects in the scene for which i replaced the prefab in project window? 1 Answer