Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 paktofu · Dec 01, 2013 at 05:46 AM · javascriptinstantiatearrays

How to spawn objects from CSV file

Hello,

I have been trying to work out a code for awhile now that reads a CSV file, parses the data, and then plots objects based on that data (x,z). Although I am able to parse the CSV file and print the array, I am not understanding how to instantiate objects based on this data.

I have hit a roadblock and really not sure what to do at this point. Does anyone have any suggestions please? Thank you

 #pragma strict
 
 //This code is used to parse a CSV file and also to spawn objects based on that CSV fi
 // Big thanks to Seth Iacangelo aka Dark Shot of FF XIV
 
 var csvFile : TextAsset;
 var prefab : GameObject;
 
 
 var x = new Array();
 var y = new Array();
 var z = new Array();
 var count = new Array();
 var id = new Array();
 
 var j = 0;
 
 function Start(){
     Longitude();
     print();
 }
 
 function Longitude () {
 
     var stringArray = csvFile.text.Replace("\n"[0],","[0]).Split(","[0]);
     
     for(var i = 3; i < stringArray.length; i+=1){
 
 
         switch (i%3)
         {
             case 0:
                 x[j] = stringArray[i];
                 break;
             case 1:
                 y[j] = stringArray[i];
 
                 break;
             case 2:
                 z[j] = stringArray[i];
                 j = j + 1;
                 break;
         }
     }
 }
 
 // Test to see if the array in longitude can be accessed from without. Eventually this code needs to spawn the prefabs
 function print(){
 
     
     for(var t = 0; t < x.length; t+=1)
     
     {
         
         var block = Instantiate(prefab);
         block.transform.position = new Vector3(0+t, 0, 0+t);
         print("The coordinates for line " + x[t] + " are " + z[t] + "." + " Height = " + y[t]);
     }
 }
Comment
Add comment · Show 1
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 Tomer-Barkan · Dec 01, 2013 at 05:30 AM 0
Share

Please edit your question and share the code that is not working otherwise we can't help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tomer-Barkan · Dec 01, 2013 at 05:51 AM

Your problem is in setting the position (assuming your CSV file is in the right format for this script). t is the iterator that you use to iterate over the array. So x[y] are the x coordinated, y[t] are the y coordinates, and z[t] are the the coordinates.

Now, when you print, you print correctly (you print x[t], z[t] and y[t]), but when setting the position, you use the iterator itself as the value (you use 0+t).

So change the position setting line to:

 block.transform.position = new Vector3(x[y], y[t], z[t]);
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

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

17 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

Related Questions

Instantiating prefabs to every object in an array? 1 Answer

Accessing variable of instantiated prefab inside array in javascript 1 Answer

Cannot add Instaniated gameObjects to a GameObject[] using Javascript. Error: "NullReferenceException: Object reference not set to an instance of an object" 2 Answers

Javascript array questions. 1 Answer

Referencing instantiated player in camera's Start() 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