Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Cube.transform.position.x

Discussion in 'Scripting' started by julien83franceschi, Dec 20, 2020.

  1. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    187
    Hello to all,

    I try to move my cube by 30 pixels on the x and 45 pixels on the z (when you click on the cube three times).

    A simple example with :

    Code (CSharp):
    1.     int i;
    2.     public GameObject Cube;
    3.  
    4.     void OnMouseDown() {
    5.  
    6.         i = ++1;
    7.  
    8.         if (i == 3) {
    9.  
    10.  
    11.             Cube.transform.position.x = 30;
    12.             Cube.transform.position.y = 45;
    13.  
    14.        //or Cube.transform.position = Vector2(30, 45);
    15.  
    16.         }
    17.     }
    I tried to change the x and z positions,
    or I tried to introduce a Vector2.

    The problem is that the compiler sends me each time an error message.

    Your help is welcome,

    A+
     
  2. unity_3V704ypuwX7kQA

    unity_3V704ypuwX7kQA

    Joined:
    Dec 20, 2020
    Posts:
    5
    Not sure but maybe try assigning a int to i (sorry if it is wrong as i am just starting to learn c#)
     
  3. julien83franceschi

    julien83franceschi

    Joined:
    Nov 14, 2020
    Posts:
    187
    Cela ne change rien, la variable est déclarée en int en haut du code.

    Merci quand même,

    A+
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Code (CSharp):
    1. Cube.transform.position = new Vector3 (30, 0, 45);
    The position vector must be replaced as a whole. You cannot assign individual components.