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
1
Question by Skibur-2 · Sep 05, 2011 at 05:21 PM · textloadread

Creating a Game Object from a text file

Hi there,

Last night I asked about saving object information to a text file, well I have managed to finish that, and loading the information back in too.

Now I am trying to figure out how to use the saved information to create the game objects.

Here's an example of the information I am loading in:

a, b, c, (d)

Where a = the type of object, b = position.x, c = position.z, d = rotation

Example of saved/loaded data:

 1,-5,-10,(270.0, 0.0, 0.0)
 1,0,10,(270.0, 270.0, 0.0)
 2,10,-10,(270.0, 90.0, 0.0)
 4,0,0,(270.0, 270.0, 0.0)
 3,10,10,(270.0, 0.0, 0.0)

What would be the best way to instantiate an object at those co-ords/rotation for each line?

I appreciate any help :)

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 bhararm · Oct 11, 2014 at 10:15 PM 0
Share

Hi @Skibur-2, I'm also trying to load and save data of the objects. Can you please provide your script or steps to create the same? Thank you for your help.

Thanks, Raj

4 Replies

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

Answer by Skibur-2 · Sep 06, 2011 at 05:03 AM

I solved it...

I just created a GameObject variable for each prefab and then assigned the prefab to that variable.

Works like a charm :D

I did want to do it an easier way, but this way works.

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
1

Answer by sven1994 · Sep 05, 2011 at 06:00 PM

I don't know what you mean with "the type of object", but I think the easiest way would be to have a prefab for each "type of object" and instantiate it. I would do it like this

 Foreach(line in linesOfFile)
 {
     Split the line by commas to get the individual parameters
     Remove spaces and brackets
     Instantiate prefab at specific position and rotation
 }

In order to achieve a learning effect I don't write you a working function. You can use google for that ;)

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 Skibur-2 · Sep 06, 2011 at 01:35 AM 0
Share

Yeah, I will handle the object type part, that's why I only asked for help with the pos/rotation.

As for splitting by comma and removing commas and brackets, I'm going to have to do some research...

Thanks for the help, and for forcing me into studying :P

avatar image
0

Answer by Skibur-2 · Sep 06, 2011 at 03:35 AM

Here's part of what I ended up using...

At the moment, when I click load everything works up to the instantiate part, when it hits an error (unable to read file) and also says that the prefab I want to instantiate is null, even if I put in "GameObject.whatever" into the function.

If I remove the instantiate part it will load the data for each line perfectly, but something about instantiate is making it just stop...

Help?

function OnMouseUp () {

     var array : GameObject[] = GameObject.FindGameObjectsWithTag("Track");
     var item : GameObject;

     for (item in array){
         Destroy(item);
         }

 try {
     sr = new StreamReader("SavedLevel.txt");
     line = sr.ReadLine();

     while (line != null) {

         var separator = ",";
         var theLine = line.Split(separator[0]);

         print(line);

         posX = (parseInt(theLine[1]));
         posZ = (parseInt(theLine[2]));
         pieceRotationX = (parseInt(theLine[3]));
         pieceRotationY = (parseInt(theLine[4]));
         pieceRotationZ = (parseInt(theLine[5]));

         print(theLine[0]);

         if ((parseInt(theLine[0]) == 1)) {
             savedTrackPiece = GameObject.trackPerpPrefab;
             }

         if ((parseInt(theLine[0]) == 2)) {
             savedTrackPiece = GameObject.trackHorizPrefab;
             }

         if ((parseInt(theLine[0]) == 3)) {
             savedTrackPiece = GameObject.trackCurvePrefab;
             }

         if ((parseInt(theLine[0]) == 4)) {
             savedTrackPiece = GameObject.trackJunctionPrefab;
             }

         var track = Instantiate(savedTrackPiece, Vector3(posX, 0, posZ), Quaternion.Euler(pieceRotationX, pieceRotationY, pieceRotationZ));

                 track.name = ("TrackPiece" + currentPiece);
         trackName = track.name;
         currentPiece ++;

         line = sr.ReadLine();
     }

     sr.Close();
 }

 catch (e) {

     print("The file could not be read:");
     print(e.Message);
 }

}

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 sven1994 · Sep 06, 2011 at 03:42 AM

Thats because trackPerpPrefab is not a member of the GameObject class.

Comment
Add comment · Show 3 · 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 Skibur-2 · Sep 06, 2011 at 03:47 AM 0
Share

Hmm, well how could I assign prefabs to those variables?

avatar image Skibur-2 · Sep 06, 2011 at 04:02 AM 0
Share

Here's what I'm trying, but it's still not working...

It's returning the correct savedTrackPiece, but the instantiate is still the problem...

         if ((parseInt(theLine[0]) == 4)) {
             savedTrackPiece = "trackJunctionPrefab";
             }

 var track = GameObject.Instantiate(Resources.Load("Level Editor/Assets/Prefabs/" + savedTrackPiece)...

Am I going down the right track?

avatar image Xerosigma · Mar 05, 2012 at 07:58 AM 0
Share

This should work for instantiating it:

var obj = Instantiate(savedObj, Vector3(posX, 0, posZ), Quaternion.Euler(objRotationX, objRotationY, objRotationZ));

Just replace my vars with yours and make sure you're setting the GameObject value of the prefab you're trying to spawn.

P.S. If you get this working let me know. Having some issues with the running multiple passes with different lines.

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

Best way to manage stats in a text file 2 Answers

Read text file from other directory 1 Answer

How to put each line of a text file into a separate sring c# 1 Answer

Null Reference Exception while trying to access Text Files 1 Answer

How to load a large text asset with minimum to no lag. 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