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 03, 2013 at 05:43 PM · c#instantiatearrays

Unity reading decimals

Hello,

I have this code that I inherited from a friend,( that I am also asking) but in case he is too busy I was hoping maybe this is a simple problem someone here can answer.

Basically I have this code that can read a CSV file and instantiate the data based on x y z and scale of y. But the problem I am having is that it is not reading decimal points. Does anyone know if this is a simple fix ?

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class Cube : MonoBehaviour {
     
     //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
 
     public TextAsset csvFile;
     public GameObject prefab;
 
     private int index = 0;
 
     private int[] x;
     private int[] y;
     private int[] z;
     
     void Start(){
         Longitude();
     }
 
     void Longitude () {
         print (csvFile);
         string[] numberArray = csvFile.text.Replace("\n"[0],","[0]).Split(","[0]);
 
         int length = (numberArray.Length - 1) / 3;
         x = new int[length];
         y = new int[length];
         z = new int[length];
 
         for(int i = 0; i < numberArray.Length - 1; i++){
             switch (i%3) {
                 case 0:
                     x [index] = int.Parse (numberArray [i]);
                 break;
                 case 1: 
                     y [index] = int.Parse (numberArray [i]);
                 break;
                 case 2:
                     z [index] = int.Parse (numberArray [i]);
                     string name = index.ToString();
                     GameObject instance = Instantiate(prefab) as GameObject;
                     instance.transform.position = new Vector3(x[index],y[index],0);
                     instance.transform.localScale += new Vector3(0,z[index],0);
                     instance.name = name;
                     index ++;
                 break;
             }
         }
     }
 }
 
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 Owen-Reynolds · Dec 03, 2013 at 05:56 PM 0
Share

You've got x,y,z declared as ints, and are using int.Parse to read them. Did the original code read ints only? And you're wanting to modify it to read floats?

Some of the problem may be that the code is a little wonky. The array isn't used for anything and the switch seems needlessly confusing (why not just read x,y,z together, and have the loop advance by i+=3?) And when do you want x&z scale to be 0, and not 1?

1 Reply

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

Answer by DaveA · Dec 03, 2013 at 05:45 PM

Use float.Parse instead of int.Parse

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 paktofu · Dec 03, 2013 at 06:55 PM 0
Share

Thank you, this is why I am new to coding. DUH lol why didn't I think of that. It works now :)

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

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

Having multiple objects fire prefabs in different times C# 0 Answers

Distribute terrain in zones 3 Answers

Remove GameObject From Array if it already exists? 1 Answer

Multiple Cars not working 1 Answer

Prefabs instantiated from an array are keeping their public int value 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