- Home /
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);
}
}
}
}
Please format your code. If you don't know how, watch the tutorial video on the right :)
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.
Thank you Coded_Velocity_$$anonymous$$evin, that works, but can somebody explain WHY?
Your answer
![](https://koobas.hobune.stream/wayback/20220613124538im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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