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 mrtrash · Dec 20, 2013 at 02:12 PM · objectspositioning

Problem with positioning objects

Hi all,

I am having a problem with my app that has 3 main actions: load scene, save scene and add sphere. The script allows the user to create and move spheres at runtime and to save and load the scene with created spheres using simple text file with saved 3d coordinates. The problem is that when I call load scene function on an empty scene the spheres are getting created and positioned correctly according to coordinates saved in teh text file however if there are any spheres added before calling the load function, the newly created spheres are positioned at (0,0,0). Things get even stranger because if the are for example 4 coordinates sets saved in a save file and there is lets say 3 spheres added before calling the load function, the 3 of the new spheres will be positioned at (0,0,0) and the last one will be positioned correctly according to saved coordinates. Can somebody please explain why is this happening?

Here is the code:

 using UnityEngine;
 using System.Collections;
 
 
 public class App : MonoBehaviour {
 
     private static int objCounter = 0;
     private static ArrayList scene = new ArrayList();
     private static ArrayList selection = new ArrayList();
 
     private static string _SaveFileName="Save.txt";
 
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public static string AddObject(string objSelector){
         GameObject obj = null;
         switch(objSelector){
             case "sphere":
                 obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                 break;
         }
         obj.AddComponent<ObjectHandler>();
         obj.name = "obj"+(++App.objCounter);
         App.scene.Add(obj.name);
 
         return obj.name;
     }
 
     public static void RemoveObject(string objName){
         App.scene.RemoveAt(App.scene.IndexOf(objName));
         Destroy(GameObject.Find(objName));
     }
 
     public static void SaveScene(){
         string SaveString = "";
         for (int i = 0; i < App.scene.Count; i++) {
             var obj = GameObject.Find ((string)App.scene[i]);
             SaveString += obj.name + ",sphere," + obj.transform.position.x + "," + obj.transform.position.y + "," + obj.transform.position.z + ";";
         }
         System.IO.StreamWriter file = new System.IO.StreamWriter(App._SaveFileName);
         file.WriteLine(SaveString);        
         file.Close();
     }
 
     public static void LoadScene(){
         while(App.scene.Count>0) {
             App.RemoveObject((string)App.scene [App.scene.Count-1]);            
         }
         App.objCounter = 0;
         string LoadString = "";
         System.IO.StreamReader file = new System.IO.StreamReader (App._SaveFileName);
         LoadString = file.ReadLine ();        
         file.Close ();
         if (LoadString.Length>2) {
             string[] objects = LoadString.Split (';');
             for (int i = 0; i < objects.Length-1; i++) {
                     string[] objData = objects [i].Split (',');
                     string objName = App.AddObject ((string)objData [1]);
                     var obj = GameObject.Find (objName);
 
                     var x = float.Parse (objData [2]);
                     var y = float.Parse (objData [3]);
                     var z = float.Parse (objData [4]);
                     obj.transform.position = new Vector3 (x, y, z);
             }
         }
 
     }
 }
Comment
Add comment · Show 3
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 Benproductions1 · Dec 21, 2013 at 04:36 AM 0
Share

Please format your code. If you don't know how, watch the tutorial video on the right :)

avatar image KevinCodes4Food · Dec 21, 2013 at 05:57 AM 0
Share

It might not fix your bug, but I'd send the object data set in it entirety into the AddObject() routine and apply positions within it, rather than passing back the name then looking up the object to modify it.

avatar image mrtrash · Dec 21, 2013 at 06:33 AM 0
Share

Thank you Coded_Velocity_$$anonymous$$evin, that works, but can somebody explain WHY?

0 Replies

· Add your reply
  • Sort: 

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

20 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 avatar image avatar image

Related Questions

How scale and position of a slider being a child object works? 0 Answers

AR Object stuck in front of camera 0 Answers

Positioning across various resolutions 0 Answers

How can I set the default position of a 3D object? 2 Answers

Moving from one object to another object either forward or backward in the SAME SCENE 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