Position is not changing in loop?

I made a save and load system to save the positions of multiple objects and my problem is my gameobjects don’t move in the loop when you load.

 public GameObject[] objects;
    [HideInInspector]
    public List<float> ObjectID = new List<float>();
    [HideInInspector]
    public List<float> Xs = new List<float>();
    [HideInInspector]
    public List<float> Ys = new List<float>();
    [HideInInspector]
    public List<float> Zs = new List<float>();
    public List<Vector3> Positions = new List<Vector3>(); //  for debuging
// my load function
public void Load()
    {
        // destroy all objects in scene
        if (GameObject.FindGameObjectsWithTag("Object").Length != 0)
        {
            GameObject[] objects = GameObject.FindGameObjectsWithTag("Object");
            for (int i = 0; i < objects.Length; i++)
            {
                Destroy(objects[i]);
            }
        }
        SaveData data = SaveSystem.LoadPlayer(); // Save data
        ObjectID = data.ObjectID; // Which object to instanciate from objects
        Xs = data.Xs; // all objects x coordinate
        Ys = data.Ys; // all objects y coordinate
        Zs = data.Zs; // all objects z coordinate


        foreach (int ID in ObjectID)
        {
            // in the foreach to not instanciate a lot of them
            GameObject NewGameobject = Instantiate(objects[ID], new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
            for (int i = 0; i < ObjectID.Count; i++)
            {
                Positions.Add(new Vector3(Xs[i], Ys[i], Zs[i])); // making new position
                Debug.Log(Positions[i]); // positions are changing
                NewGameobject.transform.position = Positions[i]; //TRYING TO SET POSITION!!!
            }
        }
    }

Any help is appreciated.

This code looks really fishy:

        foreach (int ID in ObjectID)
        {
            // in the foreach to not instanciate a lot of them
            GameObject NewGameobject = Instantiate(objects[ID], new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
            for (int i = 0; i < ObjectID.Count; i++)
            {
                Positions.Add(new Vector3(Xs[i], Ys[i], Zs[i])); // making new position
                Debug.Log(Positions[i]); // positions are changing
                NewGameobject.transform.position = Positions[i]; //TRYING TO SET POSITION!!!
            }
        }

Teh way you have this structured with the inner loop, bad things are going to happen:

For every object you instantiate, you’re adding EVERY position from your position arrays into the Positions list. and then you are setting NewGameobject.transform.position to every position in the list from 0 to ObjectID.Count.

At the end of this you will end up in this state:

  • You will have ObjectID SQUARED number of Vector3s in your Positions list
  • Every new GameObject you instantiate will have Positions[ObjectID.Count - 1] as its position (because this is what it will be set to on the last iteration of the inner loop. They will all have the same position!

That can’t be right. What are you actually trying to do?

I’m trying to get the instanciated object to go to it’s saved position. The first loop gets the object’s id and instantiate the saved object. Since every saved object has a saved X, Y, and Z (like the saved object is in objectID 0 and it’s position is in x’s, y’s, and z’s 0) I tried to make the object go to it’s position.

Why not just do this?

        for (int i = 0; i < ObjectID.Count; i++)
        {          
            Vector3 savedPosition= new Vector3(Xs[i], Ys[i], Zs[i]);
            GameObject NewGameobject = Instantiate(objects[ObjectID[i]], savedPosition, Quaternion.identity) as GameObject;
        }

By the way using floating point numbers as IDs is very unusual.

Uhhhh. I got this error. 6183554--677456--Error.PNG
if I do this in a for loop then it goes in the right position, just the wrong object was instanciated.

Sorry, I made a typo, should be a normal for loop. Corrected above.

Post your updated code?

Now it says cannot convert type ‘float’ to 'int’

also tried to fix it

for (int i = 0; i < ObjectID.Count; i++)
        {
            Vector3 savedPosition = new Vector3(Xs[i], Ys[i], Zs[i]);
            GameObject NewGameobject = Instantiate(objects[i], savedPosition, Quaternion.identity) as GameObject;
        }

Why is your ObjectID list a list of float instead of int? Is that intentional?

I like floats more. should I change it? All I know is floats can have a decimal and ints cannot (I think, never really learned the difference)

edit: changed to int and it worked! Thank You!!!

That’s true, but for Array and List indexes you must use int. It doesn’t really make sense to use float for discrete elements like that. Also float is subject to rounding errors and such.

I’m a bit confused by your whole situation here. Why are you destroying the objects and then trying to immediately Instantiate them again?

Why does it seem like you’re loading a list of object IDs from a save data somewhere, but then you’re actually getting your list of objects to instantiate from FindGameObjectsWithTag. By the way that method will not always return the objects in the same order so even if you get this working here now, it will probably break when you build the game for a platform.

Anyway you can probably just cast your floats to ints, but this is all asking for trouble:

for (int i = 0; i < ObjectID.Count; i++)
        {
            Vector3 savedPosition = new Vector3(Xs[i], Ys[i], Zs[i]);
            GameObject NewGameobject = Instantiate(objects[(int)ObjectID[i]], savedPosition, Quaternion.identity) as GameObject;
        }

I’m destroying the objects in the scene so when you load the gameobjects, there aren’t duplicates. there was a bug where when I pressed load multiple times it would make duplicates. Also if you placed anything, then loaded, it would get rid of it.