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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

(Help Me?) Moving an GameObject

Discussion in 'Scripting' started by DoruKs1, Apr 18, 2015.

  1. DoruKs1

    DoruKs1

    Joined:
    Jan 14, 2015
    Posts:
    28
    Hey Unity.
    I just want to know how to move an object with scripts. Is it something about vector3? Can you show a script that if you press "q" a cube tagged "thebox" moves?
    -sorry for bad english-
    and for your help; thanks.
     
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    there are a number of ways to "move" something in Unity
    you need to have a reference to the GameObject that has the tag.

    GameObject cubeTaggedWithTheBox = ?????

    if(Input.GetKeyDown(KeyCode.Q))
    {

    cubeTaggedWithTheBox.transform.position = new Vector3 (player.transform.position);//1
    cubeTaggedWithTheBox.Translate(Vector3.forward * Time.deltaTime);//2
    cubeTaggedWithTheBox.GetComponent<RigidBody>().ApplyForce(new Vector3(2.0f,0,0);//3
    cubeTaggedWithTheBox.transform.RotateAround(...);//4
    ...
    }
    if the script is attached to the gameobject itself which will move, then cubeTaggedWithTheBox is just gameObject or "this"
    Ex: this.transform.position....
    or gameObject.transform.position....
    or just transform.position...
     
    Last edited: Apr 18, 2015
  3. DoruKs1

    DoruKs1

    Joined:
    Jan 14, 2015
    Posts:
    28
    @TTTTTa
    Do you mean:
    GameObject.cubeTaggedWithTheBox = "a" or a
    if(Input.GetKeyDown(KeyCode.Q))
    {

    a.transform.position = new Vector3 (player.transform.position);//1
    a.Translate(Vector3.forward * Time.deltaTime);//2
    a.GetComponent<RigidBody>().ApplyForce(new Vector3(2.0f,0,0);//3
    a.transform.RotateAround(...);//4
    ...
    }
     
  4. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    I mean the latter part where you have just a, not "a".
    You cant use "a" because that is a string, not a gameObject.
    Also, you cant use "GameObject.cubeTaggedWithTheBox" because the GameObject class doesn't know what cubeTaggedWithTheBox is. you need to set a reference to the known game object that it is you are moving .

    if you have a game object in your scene, just add your script to the game object that you want to move via the inspector panel, then you can just use "this.transform..." .
    if your script is attached else where, you can use this:
    http://answers.unity3d.com/questions/24257/how-do-i-find-all-game-objects-with-the-same-name.html
    This can cause problems if you have multiple game Objects with the same tag.
    The best approach is to use the inspector panel, drag the gameObject from the hierarchy to the cubeTaggedWithTheBox variable in the script. see screen shot
    inspector.png
     
    DoruKs1 likes this.