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 Hex2013 · Mar 14, 2016 at 10:35 AM · scripting probleminstantiateprefabstreamreadertextfile

Instantiate Objects from text file

So i have a text file that is

 0:
 PT:
 7
 X:
 8
 Y:
 0
 Z:
 -4.5
 RX:
 -1.795997E-07
 RY:
 2.760271E-07
 RZ:
 2.96859E-09
 Summon
 1:
 PT:
 7
 X:
 18
 Y:
 -7.105427E-15
 Z:
 -4.5
 RX:
 -5.229958E-08
 RY:
 -1
 RZ:
 -8.977948E-07
 Summon

when it's paired with the script below they're supposed to instantiate objects into a scene, every time "Summon" is read via streamreader, but non of the values get set for some reason X,Y,Z of the position and RX,RY,RZ to set the rotation of the object.

 0:
 PT:
 7
 X:
 8
 Y:
 0
 Z:
 -4.5
 RX:
 -1.795997E-07
 RY:
 2.760271E-07
 RZ:
 2.96859E-09
 Summon


This object is the first one to be loaded, It has prefab type 7, 8,0,-4.5 is the position. And -1.795997E-07, 2.760271E-07, 2.96859E-09 is the rotation. These values are then used to instantiate an object a specific position.

Here is the script

 import System.IO;
  @header("GUI elements")
  var file : String;
  var InputText : UnityEngine.UI.InputField;
  @header ("Behind The Scenes")
  var CurrentLine : String;
  var Prefab1 : GameObject;
  var Prefab2 : GameObject;
  var Prefab3 : GameObject;
  var Prefab4 : GameObject;
  var Prefab5 : GameObject;
  var Prefab6 : GameObject;
  var Prefab7 : GameObject;
  var PrefabType : int = 0;
  var PosX : float = 0.0;    
  var PosY : float = 0.0;
  var PosZ : float = 0.0;
  var RotX : float = 0.0;
  var RotY : float = 0.0;
  var RotZ : float = 0.0;
  var PosXString : String = "X:";
  var PosYString : String = "Y:";
  var PosZString : String = "Z:";
  var RotXString : String = "RX:";
  var RotYString : String = "RY:";
  var RotZString : String = "RZ:";
  var PrefabTypeString : String = "PT:";
  var LoadSetting : int = 0;
  var SummonString : String = "Summon";
  var PosXSummon : float = 0.0;
  var PosYSummon : float = 0.0;
  var PosZSummon : float = 0.0;
  var RotXSummon : float = 0.0;
  var RotYSummon : float = 0.0;
  var RotZSummon : float = 0.0;
  var PrefabSummon : int = 0;
 
 
  function UpdateName () {
      file = InputText.text;
  }
  function ReadFile(file : String){
    file = InputText.text;
     if(File.Exists(file + ".txt")){
         var sr = File.OpenText(file + ".txt");
         var line = sr.ReadLine();
         while(line != null){
             Debug.Log(line); // prints each line of the file
             //Add load scripting here.
             CurrentLine = line;
             if(LoadSetting == 1){
                 int.TryParse(CurrentLine, PrefabType);
                 PrefabSummon = PrefabType;
             }
             if(LoadSetting == 2){
                 float.TryParse(CurrentLine, PosX);
                 PosXSummon = PosX;
             }
             if(LoadSetting == 3){
                 float.TryParse(CurrentLine, PosY);
                 PosYSummon = PosY;
             }
             if(LoadSetting == 4){
                 float.TryParse(CurrentLine, PosZ);
                 PosZSummon = PosZ;
             }
             if(LoadSetting == 5){
                 float.TryParse(CurrentLine, RotX);
                 RotXSummon = RotX;
             }
             if(LoadSetting == 6){
                 float.TryParse(CurrentLine, RotY);
                 RotYSummon = RotY;
             }
             if(LoadSetting == 7){
                 float.TryParse(CurrentLine, RotZ);
                 RotZSummon = RotZ;
             }
             if (CurrentLine == PrefabTypeString){
                 LoadSetting = 1;
             }
             if (CurrentLine == PosXString){
                 LoadSetting = 2;
             }
             if (CurrentLine == PosYString){
                 LoadSetting = 3;
             }
             if (CurrentLine == PosZString){
                 LoadSetting = 4;
             }
             if (CurrentLine == RotXString){
                 LoadSetting = 5;
             }
             if (CurrentLine == RotYString){
                 LoadSetting = 6;
             }
             if (CurrentLine == RotZString){
                 LoadSetting = 7;
             }
             if(CurrentLine == SummonString){
                 
             }
             line = sr.ReadLine();
         }  
     } else {
         Debug.Log("Could not Open the file: " + file + " for reading.");
         return;
     }
 }

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
2

Answer by toddisarockstar · Mar 14, 2016 at 07:51 PM

I had a very similair system i was using to load levels. In your text file I wouldn't separate your info by lines. I would separate by commas. this way you could use the commas in a Split function to quickly pack your numbers into an array for super easy reference. it would also make your numbers easily visable in the inspector to see where your problems are. also you could quickly identify your peramiters by array number.

 var txt:String;
 var stringarray:String[];
 var floatarray:float[];
 var i:int;
 var sr :StreamReader = new StreamReader("your path");                  
 txt = sr.ReadToEnd();
 sr.Close();
 
 //make an array of string numbers from your text file
 stringarray=txt.Split(","[0]);
 
 
 //make another array of floats from the string;
 floatarray=new float[stringarray.Length];
 i=stringarray.Length;
 while(i>0){i--;
 float.TryParce(stringarray[i],floatarray[i]);
 }
 

 

in your text file there should be no need for anything but numbers and commas. if you write the numbers in the same order everytime your script should always know that floatarray[2] is always a y coordinate and so on. you wouldnt need all those variables you have. each "load" would just be a neat array. I hope this concept makes things easier for ya. i would just keep a little notebook by your keyboard to remember which numbers in the array are for what when your doing your spawning and stuff.

if your script is not parcing some of those floats correctly it is my guess the tryparce function doesnt like the "e"'s in your string. when you write your rotations and positions you could eliminate that by rounding some of the numbers to the nearest .001 like this:

 var numbertowrite:float;
 numbertowrite=Mathf.Round(transform.position.x*1000)*.001;

 
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate a prefab and then add a animator component and controller to the component. 2 Answers

Script Not Working On Prefab 2 Answers

Put a prefab in Hierarchy without using Instantiate ? (from code) 2 Answers

How to script independent time tracking of prefabs? 1 Answer

My prefab isn't getting destroyed 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