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 Powarader · Oct 22, 2018 at 06:24 PM · gameobjectsinstantiation

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?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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:

  1. You don't need to instantiate anything until the player places them in the world

  2. They can be built

Comment
Add comment · Show 1 · 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 Powarader · Oct 27, 2018 at 10:06 AM 0
Share

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

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

95 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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