- Home /
The question is answered, right answer was accepted
Multiple Buildings Placement
Hello. I have a building system in place and buttons that act like the building image and etc. When I press the button, the building instantiates correctly, it can be placed correctly, everything works fine.
However when the building is placed then that's it, I cannot place the building again unless I press the button again and I know it does that because when the building is placed the prefab holder is set to null.
Here are two code snaps:
This one is used to place the building:
public void PlaceFixture()
{
if (Input.GetMouseButtonDown(0))
{
fixtureToPlace.layer = 0;
surface.BuildNavMesh();
fixtureToPlace = null;
isPlaced = true;
}
}
private void Update()
{
if(isBuilding)
{
Time.timeScale = 0;
visualGrid.SetActive(true);
MoveObjectToMousePosition();
RotateObject();
PlaceFixture();
if (Input.GetMouseButtonDown(1))
{
Destroy(fixtureToPlace);
isPlaced = false;
fixtureToPlace = null;
}
}
}
And this is the one used for the button:
public void PlaceFixture()
{
buildingSystem.isBuilding = true;
buildingSystem.fixtureToPlace = Instantiate(modelPrefab);
modelPrefab.name = "Shelf";
}
The script above is used in a prefab that gets it's information from a scriptable object database and I cannot simply simply say in the first script to just repeat that because it needs to search for that specific item in the list.
My question is what can I do/change to instantiate another building immediately after the first one is placed without clicking the button multiple times?
Thanks in advance!
Answer by RX187 · Aug 31, 2018 at 03:26 AM
Eh, nevermind, I fixed it quite easily:
if(isPlaced)
{
isPlaced = false;
fixtureToPlace = Instantiate(fixtureToPlace);
}
And in the script that has the button
public void PlaceFixture()
{
buildingSystem.isBuilding = true;
buildingSystem.fixtureToPlace = Instantiate(modelPrefab);
buildingSystem.fixtureToPlace.layer = 2;
modelPrefab.name = "Shelf";
}
Just in case someone needs it.