Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by 4illeen · Aug 03, 2011 at 07:34 AM · gameobjectinstantiateprefabdatainformation

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CHPedersen · Aug 03, 2011 at 07:43 AM 0
Share

Do the instantiated buildings have scripts attached to them in the first place?

avatar image 4illeen · Aug 03, 2011 at 07:48 AM 0
Share

at the moment no, the player has everything on him right now

avatar image CHPedersen · Aug 03, 2011 at 07:53 AM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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:

  1. Define a GameObject called "Building".

  2. Make a script called "BuildingScript" or something, and attach it to the Building GO.

  3. In the script, define all the booleans, floats, strings, etc. that make up the characteristics that define a building.

  4. 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.

  5. 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.

  6. 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
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image save · Aug 03, 2011 at 11:28 AM 0
Share

Fantastic answer!

avatar image biohazard · Aug 03, 2011 at 11:29 AM 0
Share

o hai

avatar image 4illeen · Aug 04, 2011 at 08:53 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges