• 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
2
Question by Shali · Aug 20, 2013 at 12:30 PM · gameobjectruntimexml

Load game objects at runtime?

Hi Guys!

I'm very new to unity3D, would someone please tell me what things i need to do to, load game objects as we play the game, for example let's say there's a parent object with several spheres in it each containing one child entity that is a 3Dtext, and ofcourse these are not in the hierarchy but in a separate XML file or stored in different similar fashion. Any guidance is very much appreciated.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Yokimato · Aug 20, 2013 at 12:39 PM

When you talk about the XML files I assume you're storing data that ultimately ends up in a script. The gameobject themselves can be created multiple ways. The easiest scenario:

Say you have a FighterTank object with an XML data backing for it's stats. You could create a gameobject give it a tank mesh and add a FighterTank script to it that has, let's say, Health, Damage and Range variables. Save your gameObject as a prefab. Your XML might look something like:

 <xml>
  <fightertank>
     <health>100</health>
     <damage>200</health>
     <range>75</range>
  </fightertank>
 </xml>

After reading in the xml, you can create your prefab at runtime using the Instantiate function. Once created, call GetComponent on the created prefab to request your FighterTank script you added. From there, you can simply assign your Health, Damage and Range variables.

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 Shali · Aug 20, 2013 at 12:51 PM 0
Share

Thank you very much Yokimato, i'll give it a shot.

Cheers!

avatar image
-1

Answer by $$anonymous$$ · Aug 20, 2013 at 12:34 PM

http://en.lmgtfy.com/?q=unity+Load+game+objects+at+runtime

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

Answer by Cygon4 · Aug 20, 2013 at 01:02 PM

The standard solution for this within Unity are "Prefabs." It works like this:

  • First you design your game object as you want it directly in the Unity Editor (including child objects, as you described)

  • Then you create a Prefab (right-click in Project tree, Create -> Prefab and drag the game object you built from the Hierarchy tree onto the Prefab.

You can now delete the game object from the scene. The Prefab is like a blueprint - you can "clone" this Prefab into your scene via script, any number of times, wherever you want.

For example, an Enemy Spawner script might have a slot onto which you can drag a enemy Prefab of the kind of enemy that should be spawned. Or a weapon script might have a slot onto which you drag a prefab for the kind of projectile (bullet, rocket, whatever) the weapon should create when it is fired.

Comment
Add comment · Show 6 · 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 Shali · Aug 20, 2013 at 01:06 PM 0
Share

Thnaks Cygon4, very nice of you, highly appreciate it.

avatar image fafase · Aug 20, 2013 at 01:06 PM 0
Share

He has the object description as xml file. SO he needs a xml parsing method first.

avatar image Shali · Aug 20, 2013 at 01:41 PM 0
Share

Hi, i've just written a c# script and got it to work, one sphere created and new ones spawning at runtime according to keystroke. BTW fafase, i havent done anything using X$$anonymous$$L yet, it was just an idea and an example to describe my situation, also what do you guys think is the best approach to stored data externally that are been created i.e the ones that are been spawned at the initial run and have them saved for the next session as well? Thanks for helping me out guys.

avatar image Cygon4 · Aug 20, 2013 at 07:20 PM 0
Share

fafase: he wrote "but in a separate X$$anonymous$$L file or stored in different similar fashion" - it sounded like he might just be looking for a general way of storing object hierarchies as prepared object configurations.

In that case, it would have been cri$$anonymous$$al not to at least mention that this is a built-in, first class feature of Unity already :D

avatar image Cygon4 · Aug 20, 2013 at 07:39 PM 0
Share

Shali: saving the locations and state of objects at runtime would indeed require creating your own save solution using X$$anonymous$$L or another format.

So far I have snuck out of the situation myself by using save points. For full scene state saving there would have to be a higher-order manager to which all dynamic objects in a scene are registered (so that when the time to save comes, it knows what to save) and which has prefabs for each type of dynamic object that can be restored in a scene when it is loaded again.

Alternatively, the scene could be enumerated and saveable objects would have to be recognized somehow (tags, a special component or such) - though that would split state saving and restoring apart, requiring both sides of the code to be carefully kept in sync.

There are several solutions in the Unity Store, too, that may bring you some or all of the way there :)

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

using WWW to upload prefabs/game objects in webplayer... help 1 Answer

Help on making a Database. 0 Answers

Pass a GameObject to a DLL 2 Answers

Questions about XML save/load 0 Answers

GameObject references runtime script in scene file. Unsure whats wrong 2 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