I have a class “building” which several other classes extend, the building class contains and abstract method “newTurn()”. All the buildings in question are child objects to a Colony object.
I have a button that ends the turn, this uses broadcastmessage(newturn) on colony, so by my understanding it sends the message to colony and all of it’s children (buildings).
so far all of my building scripts other than HQ have an empty override for newTurn(). When I build seperate buildings through the HQ, it mostly runs fine. However, when i try to build a second building of the same type, for some reason the newTurn method in HQ is now being called twice. Building other buildings afterwards is fine once again only one call per script.
I only have one HQ building, yet when i try to make, say a barracks… if there is already one in the colony, the newTurn() method in the HQ script is called twice for each single press of the button.
I have absolutely no idea whats going on here, i assume it’s something to do with Broadcastmessage that i don’t understand.
Can any one help shine some light on this? Thanks for your time all.
-edit: points that might be worth mentioning:
1)The second building is an instantiated prefab, so where as the first might be “barracks” the second is then called “barracks (clone)”.
2)the gameobject of the building being built is not set active until its scripts “turns in production” variable reaches required number.
3)making a third building of the same type does not make the method be called three times, still only two. Leading me to assume it is calling once for the actual HQ, and then once for the building it is producing IF it is a prefab clone.
Important - i believe i have confirmed that as it go’s through each building calling new turn, it calls HQ new turn INSTEAD of the prefab clone’s new turn, probably because the gameObject is deactive.
Solved but don’t fully understand - I resolved the problem by only making the new building be a child of the colony after it was active, BUT i would like to understand why broadcast message deflected onto HQ when it couldn’t find it’s intending target.
I can only assume that it is something to do with the building being instantiated within the HQ class, and the HQ class holds the only reference to it until it is activated.
buildingToMake = (GameObject)Instantiate(Resources.Load("Prefabs/Buildings/Imperial buildings/" + itemName));
the building is then manipulated through buildingToMake in HQ up until the point when it is built and operates on it’s own.