- Home /
Empty GOs as info storage
In my game player is able to instantiate various buildings and objects in front of him (aka build stuff). The thing is I need them to hold some info about their properties (like some objects cannot be instantiated indoors, some need to be built on other objects etc).
I see two ways to solve this problem, one through scripting - which seems a bad idea to me, probably because I'm a unity rookie - is to make a new class BuildingInfo and hold all the bools inside. But as far as I know, I'd have to add this script to each prefab and then use a giant switch while instantiating to check objects name and set those variables.
My second idea was to add empty GOs to the prefabs with descriptive names like ThisCannotBeBuiltIndoors, and simply check for children just like I'd check if the bool was true or false. The real question is - are empty game objects somehow bad for the memory usage/performance/other, if there are few of them on each prefab?
Is there any other easier way around I cannot see?
Do the instantiated buildings have scripts attached to them in the first place?
at the moment no, the player has everything on him right now
In that case, I would make a script and attach it to the prefab (the buildings are prefabs, right?). How many different kinds of buildings are there?
Answer by CHPedersen · Aug 03, 2011 at 09:27 AM
Having Unity create an empty GO just to use its name to define some sort of behavior is huge overkill and a quite odd way to go about solving this, I think. :) Like save pointed out in the above, you're not making individual scripts for each combination. Instead, you're setting variables in each script instance to represent the characteristics of that particular building.
Here's the general idea, more elaborately explained:
Define a GameObject called "Building".
Make a script called "BuildingScript" or something, and attach it to the Building GO.
In the script, define all the booleans, floats, strings, etc. that make up the characteristics that define a building.
In the script, define a method that takes an argument describing the building type, and then initializes the building's variables to some particular preset based on that argument.
Make an empty prefab, and drag the Building GameObject onto it, to make a general Building Prefab, then delete the Building GameObject. Now you have a Building prefab, each clone of which is going to instantiate with its own instance of BuildingScript.
When you instantiate clones of the Building prefab, call the method that initializes its variables with an argument that defines what particular building type you want this clone to be.
You didn't mention which programming language you're using, so I'm going to take the liberty to choose C# for the example. ;) Hope it's alright, if not, I'm sure you can translate it to whatever you use. Here's what the BuildingScript could look like:
using UnityEngine;
using System.Collections;
public class BuildingScript : MonoBehaviour {
public bool CanBeInDoor { get; set; }
public bool ShouldBeStackedOnOtherBuilding { get; set; }
public bool IsTheBabelTower { get; set; }
public bool EifelWouldBeProud { get; set; }
public void Initialize(string buildingType)
{
switch (buildingType)
{
case "Babel":
CanBeInDoor = false;
ShouldBeStackedOnOtherBuilding = false;
IsTheBabelTower = true;
EifelWouldBeProud = true;
break;
case "EmpireState":
CanBeInDoor = false;
ShouldBeStackedOnOtherBuilding = false;
IsTheBabelTower = false;
EifelWouldBeProud = true;
break;
case "HumbleHut":
CanBeInDoor = false;
ShouldBeStackedOnOtherBuilding = true;
IsTheBabelTower = false;
EifelWouldBeProud = false;
break;
case "NonLivableDwelling":
CanBeInDoor = true;
ShouldBeStackedOnOtherBuilding = true;
IsTheBabelTower = false;
EifelWouldBeProud = false;
break;
case "DollHouse":
CanBeInDoor = true;
ShouldBeStackedOnOtherBuilding = false;
IsTheBabelTower = false;
EifelWouldBeProud = false;
break;
default:
break;
}
}
}
Here is what it would look like when you instantiate buildings:
public class ThisIsSomeOtherScript : MonoBehaviour
{
public Transform BuildingPrefab; // Drag the prefab onto this variable in the editor
void CreateTwoCopies()
{
Transform dollHouseInstance = (Transform)Instantiate(BuildingPrefab);
dollHouseInstance.GetComponent<BuildingScript>().Initialize("DollHouse"); // This now has all of DollHouse's characteristics
Transform empireStateInstance = (Transform)Instantiate(BuildingPrefab);
empireStateInstance.GetComponent<BuildingScript>().Initialize("EmpireState"); // This now has all of EmpireState's characteristics
}
}
I had a break from work for few days and now when I wanted to implement this I found out that the check if the building can be built indoors has to be done BEFORE the instantiation.
Your answer
Follow this Question
Related Questions
Retrieving GameObjects from a Prefab 1 Answer
Instantiated objects always at position 0,0,0 0 Answers
Spawning a prefab at another object's location 3 Answers
I cant delete multiple instantiated prefabs 0 Answers
Interchangeable Objects/Prefabs 1 Answer