- Home /
How can I instantiate a runtime created object
Hello there reader!
I'm currently working on a Game that has a feature in which one can create an object (a building) at runtime.
After clicking the button "Finish", the camera goes back to the world, and the created building should be attached to the mouse, the building creation gets destroyed. In the meantime, between destroying and making it attached to the mouse I want my script to iterate through all objects in the building and create a gameObject variable without instantiating it (i will instantiate it in another script).
Here's some of my code:
BuildingCreator:
public GameObject Building()
{
GameObject building = new GameObject();
building.transform.position = Vector3.zero;
foreach (GameObject block in buildingBlocks)
{
Debug.Log("Entered block Loop");
GameObject localBlock;
localBlock = block;
localBlock.GetComponent<MeshRenderer>().material = placerMaterial;
localBlock.transform.parent = building.transform;
Debug.Log(localBlock);
}
foreach (GameObject window in buildingWindows)
{
GameObject localWindow;
localWindow = window;
localWindow.GetComponent<MeshRenderer>().material = placerMaterial;
localWindow.transform.parent = building.transform;
}
foreach (GameObject roof in buildingRoofs)
{
GameObject localRoof;
localRoof = roof;
localRoof.GetComponent<MeshRenderer>().material = placerMaterial;
localRoof.transform.parent = building.transform;
}
return building;
}
My problem is when I set a variable in another script:
public void SetObjToBuilding()
{
obj = buildingCreator.Building();
}
The obj gets instantiated (first i don't want that, i only want to store it) and besides instantiating, it just empty..
Any help guys?
Answer by GamitusLabs · Oct 22, 2018 at 06:38 PM
Well, it looks like you create one empty GameObject (building) and then create a bunch of local variables which are never assigned to anything or instantiated, and are therefore lost on exiting the scope of the function.
One solution would be to save a"blue-print" of said building so that:
You don't need to instantiate anything until the player places them in the world
They can be built
Didn't realized you've answered!
Thank you for your answer though I didn't fully underestand it.
When I say that, for example,
localBlock = block
he block value is actually a previously Instantiated object that exists in the world at runtime, kind of like a building constructor at runtime.
How would I go and create that "blue-print" you mentioned? ($$anonymous$$inda makes me think of another game engine lol)
Are you mentioning something like a Prefab that can be modified at runtime?
Your answer
Follow this Question
Related Questions
Instantiate an array of gameobjects with a time delay between each 1 Answer
doubt Raycastall Instantiate object 1 Answer
Add a GameObject to a list OnCollisionEnter and remove it from the list OnCollisionExit 1 Answer
how to hide mutiple gameobjects that are in an area? 0 Answers
How do i change a game object in c# 0 Answers