Hi there all, I see that this question has been asked before, but the question pertained to removing replacing one game object, and I need to do this for multiple child game objects. There are 2 goals I am trying to accomplish with this topic, and they are as follows…
- The script will replace the proxy game objects with houses that I am in the process of making more than the 3 that I have.
- The script will also randomly pick the list of houses, which are public properties that I can add different style houses.
So far, I have the script attached to the parent game object, which is called “buildingLots.” The script, written in C#, can loop through the child proxy game objects, and for now, can instantiate one house style. However, I am running into a problem where only half of the proxy game objects are being replaced. Then, after thinking about it, it makes sense why this is happening. Basically, what you are doing is deleting objects and looping at the same time, which obviously you cannot do without looping in a reverse direction. However, even though C# can iterate in a reverse direction, intellisense does not list a reverse method while doing foreach loop. I am attatching at the bottom here my script, for whaat I have so far, and maybe someone could help.
public GameObject[] replacingBuildings;
// Use this for initialization
void Start()
{
if (transform.childCount > 0)
{
foreach (Transform proxy in transform)
{
GameObject newBuilding = Instantiate(replacingBuildings[0]);
newBuilding.transform.position = proxy.gameObject.transform.position;
newBuilding.transform.rotation = proxy.gameObject.transform.rotation;
//newBuilding.transform.localScale = tProxy.gameObject.transform.localScale;
newBuilding.transform.parent = proxy.parent;
DestroyImmediate(tProxy.gameObject);
}
}
else
Debug.Log("BuildingLotArea needs to have its buildings set as children.");
}