Creating and Moving objects

Hello, I have a problem with moving objects created by script `Instantiate(toCreate120730-przechwytywanie.png

The problem is that they edit Transform in the folder instead of in the game.
There’s a code of Move Object:

using UnityEngine;
using UnityEngine.UI;

public class MovingObjects : MonoBehaviour {
    public GameObject[] toMove;
    public InputField[] xyz;
    public int toMoveNumber;
    public bool isMoving;
    public Toggle toggle;
    public Dropdown dropdown;
    private void Update()
    {
        isMoving = toggle.isOn;
        toMoveNumber = dropdown.value;

        if (!isMoving)
        {
            xyz[0].text = toMove[toMoveNumber].transform.position.x.ToString();
            xyz[1].text = toMove[toMoveNumber].transform.position.y.ToString();
            xyz[2].text = toMove[toMoveNumber].transform.position.z.ToString();
            xyz[3].text = toMove[toMoveNumber].transform.rotation.y.ToString();
        }
        else
        {
            toMove[toMoveNumber].transform.position = new Vector3(float.Parse(xyz[0].text), float.Parse(xyz[1].text), float.Parse(xyz[2].text));
            toMove[toMoveNumber].transform.rotation = new Quaternion(0, float.Parse(xyz[3].text), 0, 0);
        }
    }

}

And there’s a code of Create Object:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CreatingObjects : MonoBehaviour {
    public GameObject[] toCreate;
    public Dropdown toMoveDB;
    public Dropdown toCreateDB;
    public Transform spawnPosition;
    public int value;
    private MovingObjects movingObjects;

    private void Start()
    {
        movingObjects = GetComponent<MovingObjects>();
    }

    public void OnButtonClick()
    {
        Instantiate(toCreate[toCreateDB.value], spawnPosition);
        toMoveDB.AddOptions(new List<string> { toCreate[toCreateDB.value].ToString() });
        movingObjects.toMove.SetValue(toCreate[toCreateDB.value], value);
        value++;
    }
}

What’s wrong here?

The problem appears in the ‘OnButtonClick’ method on line 20. You instantiate a gameObject in the scene but hold no reference to it. So, on line 22 when you when you are calling ‘SetValue,’ you are passing in the prefab of the object instantiated, not the instantiated object (the object in the scene) itself.

‘Instantiate(toCreate[toCreateDB.value], spawnPosition)’ returns the gameObject created. You should use the returned value rather than the prefab when setting the object you want to move. I suggest you do something like this:

public void OnButtonClick()
{
        // Reference to instantiated
        GameObject obj = Instantiate(toCreate[toCreateDB.value], spawnPosition);
        toMoveDB.AddOptions(new List<string> { toCreate[toCreateDB.value].ToString() });
		// Pass instantiated here
        movingObjects.toMove.SetValue(obj, value);
        value++;
}

I hope this helps

You need make a variable of your gameobject when you are instanting a prefab like this :

GameObject someGamebject = Instantiate(toCreate[toCreateDB.value], spawnPosition);