- Home /
Instantiate an instance?
Hi,
I'm about to do something i don't know is accurate. It works, as well it seem so on the surface, but I'm not sure of the implications this method might have. What I am trying to achieve is to keep adding new instances of a prefab when holding down Shift-key (like you do when plopping out multiple copies of a building in a city-builder).
This is a but pseudo code-ish. In my real code theres a lot more going on, like the building prefab following the mouse, checking for valid ground to place etc, but this demonstrates the issue.
GameObject buildingGO;
void PlopBuilding(string prefabName){
buildingGO = Instantiate( Resources.Load(prefabName), pos, rot ) as GameObject;
}
void PlopBuilding(GameObject go){
buildingGO = Instantiate( go, pos, rot ) as GameObject;
}
void Update(){
if ( Input.GetButtonDown(0) ){
PlopBuilding("Prefab Name");
if( Input.GetKey(KeyCode.LeftShift) ){
PlopBuilding( buildingGO );
}
}
}
So, this will instantiate the instance reference. Will the new instance be fully unique or will this result in some weird connection between the two instances that might result in errors further down?
Thank you!
Answer by pako · Jan 09, 2018 at 09:36 PM
Each GameObject instance you create will be unique.
You can check it by using GetInstanceID
after instantiating:
void PlopBuilding(string prefabName){
buildingGO = Instantiate( Resources.Load(prefabName), pos, rot ) as GameObject;
Debug.Log("Instantiated prefabName: " + prefabName + " - InstanceID: " + buildingGO.GetInstanceID() );
}
void PlopBuilding(GameObject go){
buildingGO = Instantiate( go, pos, rot ) as GameObject;
Debug.Log("Instantiated GameObjectName: " + go.name + " - InstanceID: " + buildingGO.GetInstanceID() );
}
Your answer
Follow this Question
Related Questions
Instantiate question 2 Answers
How to instantiate once when boolean changes 1 Answer
Instantiate a Prefab as a Child? 5 Answers
Instantiated prefabs acting different than those dragged onto scene 0 Answers