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 smoenkhoff · Sep 28, 2011 at 03:27 AM · javascriptgameobjectprefabcreation

Object Creation

I have a problem that I cannot seem to solve. I want to fetch a file or files from a URL and parse each line in said file(s) to create new objects in my game.

A line looks like this:

rock_001,1.5,0,.01,0,0,0,1,1,1

where rock_001 is the prefab name
the next 3 elements (1.5,0,.01) are the position
the 2nd set of 3 elements (0,0,0) is the rotation
and the 3rd set of 3 elements (1,1,1) is the scale

I've successfully fetched and parsed the file from the URL then I pass it off to the following function.

I have the following code that works but it basically creates an empty game object (object with no substance - mesh, colliders, and certainly not the prefab as defined by info[0], etc).What I'm having difficulties with is info[0] contains the "rock_001" value in which I have a prefab called that. Is there a way to instantiate this prefab? I would rather not create giant if else/case statement for all the prefabs possibilities.

 static function CreateObject(line : String) {
 
     // define the variables
     var position : Vector3;
     var rotation : Quaternion;
 
     // parse the line into it's elements
     var info = line.Split(","[0]);
 
     // simple debug output to verify elements of the input line
     for(var i = 0;i<info.length;i++) {
         Debug.Log("info["+i+"]: "+info[i]);
     }
 
     // define variables based on the input line
     var newObj : GameObject = new GameObject(info[0]);
     var px : float = parseFloat(info[1]);
     var py : float = parseFloat(info[2]);
     var pz : float = parseFloat(info[3]);
     var rx : float = parseFloat(info[4]);
     var ry : float = parseFloat(info[5]);
     var rz : float = parseFloat(info[6]);
 
     //create position and rotation based on the input line elements
     position  = Vector3(px,py,pz);
     rotation = Quaternion.Euler(rx,ry,rz);
 
     //assign the position and rotation to the new object
     newObj.transform.position = position;
     newObj.transform.rotation = rotation;
 
     Debug.Log("newObj: "+newObj.ToString);
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by jonas.du · Sep 28, 2011 at 02:07 PM

If you place your prefabs in a folder called 'Resources' you can load them dyamicly like this:

  var rock:GameObject = Recources.Load("rock_001");

Where 'rock_001' is the name of the prefab you want to load. Read this part of the documentation.

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 SilverTabby · Sep 28, 2011 at 04:00 AM

It appears that you are looking for either a [Hashtable][1] or a [Dictionary][2]. These are collections of objects that allow you to access them based on a key (usually a string - hey look at that)

Advantages to these types of data structures:

  • Can store a varibable number of Objects

  • You can refer to each object via a string, for example "rock_001"

  • Finding, adding, removing, retrieving elements from the list is easy and efficient

Keep in mind that these have a very high memory overhead in their use.

The example code in the linked reference (near the bottom) is written in C#, so here's how to create and work with a Hashtable in Javascript:

 import System.Collections.Hashtable;
 var prefabs : Hashtable = new Hashtable();
 var rockPrefab : GameObject;
 
 function Start()
 {    
     addEntry( "rock_001", rockPrefab); 
     Debug.Log(  (prefabs["rock_001"] as GameObject).name  );
 }
 
 function addEntry( key : String, value : GameObject)
 {    prefabs[key] = value;    }
 
 function createObject( name : String )
 {
     Instantiate( prefabs[name] as GameObject, transform.position, transform.rotation);
 }

One thing you have to be careful about using Hashtables is that you ONLY use one type of objects as keys (String) and that you ONLY put one type of object into it (choose GameObject or Transform and stick with), and that you ALWAYS cast the result of the Hashtable to the correct type of object.

The Dictionary takes care of this problem by using Generics (see the example code at the bottom of the links). The Dictionary makes sure that you never accidentally put a Rigidbody in where a Transfrom should be. [1]: http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx [2]: http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

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 smoenkhoff · Sep 28, 2011 at 12:05 PM 0
Share

Thanks for the answer. I come from a Java background so I'm familiar with how both Dictionary and Hashtable function but was hoping to avoid using them because of their memory intensiveness. I was hoping to just create the prefabs on the fly verses store "originals" in a data structure and clone them when needed.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How would I change the value of one prefab's variable via script without changing all of them? 1 Answer

When the original enemy dies, the spawns stop (javascript) 2 Answers

Prints correct on console but not updating on Prefab/Gameobject. 2 Answers

Destroying a prefab on collision with a cube? 1 Answer

Instantiating an Object... 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