How to Change position of gameObjects in unity by C# script

Unity have a class for holding and changing value of a gameObject that is transform, by this class you can change position, rotation, and size of a gameObject. transform have three methods for changing position of a gameObject. Let's take a look at all three methods -

1. transform.Translate() Method

First method that transform class have is transform.Translate(). This method is useful when you want to move your gameObject to particular position so this method directly move your gameObject to given position.

Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    // Start is called before first frame update
    void Start()
    {
       transform.Translate(1,1,1);// change position to 1,1,1
       
    }
}

By calling this method you have to enter x, y, and z coordinate in Translate method as seen above in code example transform.Translate(1,1,1); . as soon as we run our scene it will change the position of gameObject in which this script is attached.


alternatively you can also pass a vector in this method.

Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    private Vector3 pos = new Vector3(1,1,1);
    // Start is called before first frame update
    void Start()
    {
       transform.Translate(pos);
       
    }
}

Changing position gameObject other than attached script

to change position of other gameObject, you can declare GameObject and then call this by using declared gameObject name adding dot "." and then method  cube_object.transform.Translate(pos);

Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    public GameObject cube_object;
    private Vector3 pos = new Vector3(1.2f,2.2f,3.0f);
    // Start is called before the first frame update
    void Start()
    {
       cube_object.transform.Translate(pos);
       
    }
}

 now drag and drop gameObject you want to move.

How to Change position of gameObjects in unity by C# script

2. tansform.position 

This method can change position of gameObject according to world position instead of local position means if gameObject is a child of other gameObject then its position will be according to world position.

Code :
using UnityEngine;

public class test : MonoBehaviour
{
    public GameObject cube_object;
    private Vector3 pos = new Vector3(1,1,1);
    // Start is called before first frame update
    void Start()
    {
       cube_object.transform.position=pos;
    }
}

You have assign a vector value with it.


if you want position of a gameObject then you can declare a vector and assign it by GameObject.transform.position  it will return a vector value.

Code :
using UnityEngine;

public class test : MonoBehaviour
{
    public GameObject cube_object;
    private Vector3 pos;

    void Start()
    {
       pos = cube_object.transform.position;  
    }
}

if you want to  value only one axis x, y, or z then you can declare a float and assign it by GameObject.transform.position.x   you can get value by change x to y, or z for getting your desired value. 

Code :
using UnityEngine;

public class test : MonoBehaviour
{
    public GameObject cube_object;
    float x;
    float y;
    float z;

    void Start()
    {
        float x = cube_object.transform.position.x;
        float y = cube_object.transform.position.y;
        float z = cube_object.transform.position.z;
    }
}


You can also use this method for changing position at fixed value from it current position then you can use following code.

Code :
using UnityEngine;

public class test : MonoBehaviour
{
    public GameObject cube_object;

    void Start()
    {
       cube_object.transform.position+=new Vector3(1,0,0);
    }

}

cube_object.transform.position+=new Vector3(1,0,0); this will increase position of x axis of gameObject by 1 unit. so += add value of x, y and z in its current position.

3. transform.localPosition

This method is very useful because it change position locally so if gameObject is a child object then it will change position according to its local transform instead of world transform.

Code :
using UnityEngine;

public class test : MonoBehaviour
{
    public GameObject cube_object;
    private Vector3 pos = new Vector3(1,1,1);

    void Start()
    {
       cube_object.transform.localPosition=pos;
    }
}


You can also increase or decease position of gameObject from its current position to particular unit cube_object.transform.localPosition += pos;

Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class test : MonoBehaviour
{
    public GameObject cube_object;
    private Vector3 pos = new Vector3(1,1,1);
    // Start is called before first frame update
    void Start()
    {
       cube_object.transform.localPosition += pos;
    }
}


For accessing current position in a vector or particular axis you can use following code

Code :
using UnityEngine;

public class test : MonoBehaviour
{
    public GameObject cube_object;
    private Vector3 pos;
    float x;
    // Start is called before first frame update
    void Start()
    {
       pos = cube_object.transform.localPosition;
       x = cube_object.transform.localPosition.x;
    }
}


d/w  transform.position and transform.localPosition

As above we have seen two methods one is transform.position and other is transform.localPosition both are same and work same but both have some differences.

There a two type of position coordinate in unity one is world position and other is local position. 

World Position :- When you create a gameObject it have a default transform component attached to it. If gameObject is not a child object then it is its world postion. 

Local Position :- Now we create an other gameObject  gameObject and make it a child of any other object whose postion is not (0,0,0) then we notice that coordinate in tansform in inspector window are changed which respect to it parent object so now it is its local position.

so transform.position set coordinate according to world position which is sometime is our desired result but sometime not.

and transform.localPosition chnage coordinate according to it local position due to it parent gameObject.


Comments