0
\$\begingroup\$

I'm trying to move an object to the coordinates 48.7, 51.07, 102.8, but it does not move. I did some test to make sure that "OnMouseDown" works, and it does, I think it could not be finding the object "upgradeone".

public class click1 : MonoBehaviour {
    GameObject Upgradeone;
    GameObject cube;

    // Use this for initialization
    void Start () {
        Upgradeone = GameObject.Find("upgradeone");
        cube = GameObject.Find ("Cube");
    }

    void OnMouseDown(){
        Upgradeone.transform.localScale = new Vector3(48.7f,51.07f,102.8f);
    }
}
\$\endgroup\$
1

3 Answers 3

3
\$\begingroup\$

I'm certainly seeing some issues in the way you're using Unity and with the script you have.

You script to move the object should be attached to the object you want to move. This means you won't need to use Find to statically locate the object you want to move, it'll be the object the script is attached to. That means you can simply use the this keyword to access the object to be moved.

Further, you're setting the local scale of the object, not the position. You'll want to modify this.transform.position to adjust the position of the object in the game world.

Finally, you're only taking action in OnMouseDown this only triggers once when the object is clicked. If you want to move the object over time, you'll have to use the Update method. You can still use OnMouseDown to trigger the start of the movement.

As an example, this script will move the object to the coordinates when clicked:

using UnityEngine;
using System.Collections;

public class click1 : MonoBehaviour {

    public float speed = 1;
    public Vector3 target = new Vector3(48.7f,51.07f,102.8f);
    private bool moving = false;

    // Update is called once per frame
    void Update () {
        if(moving) {
            float step = speed * Time.deltaTime;
            transform.position = Vector3.MoveTowards(transform.position, target, step);
        }
    }

    void OnMouseDown(){
        moving = true;
    }
}

Or if you wanted it to be instantaneous and activated from a different object:

using UnityEngine;
using System.Collections;

public class click1 : MonoBehaviour {

    public GameObject objectToMove;
    public Vector3 target = new Vector3(48.7f,51.07f,102.8f);

    void OnMouseDown(){
        if(objectToMove != null) {
            objectToMove.transform.position = target;
        }
    }
}

Then you set objectToMove by dragging the object from the hierarchy into the inspector for your trigger object, specifically the objectToMove field of the click1 script.

Or, you can set in the Start method with the Find method, but I'd recommend against it. Setting the objectToMove in the editor is a cleaner way of dealing with this and doesn't hard code strings into your scripts.

\$\endgroup\$
2
  • \$\begingroup\$ I just want it to instantly move there, and is there a way to move it when the script is not attached to it? \$\endgroup\$
    – Caleb
    Mar 13, 2015 at 16:28
  • \$\begingroup\$ Sure, see my update. \$\endgroup\$
    – House
    Mar 13, 2015 at 16:40
1
\$\begingroup\$

There are a couple ways to move an object in Unity:

  • obj.transform.Translate(theTranslationVector)
  • obj.transform.position = newPosition

In the code you are using, you set the object's localscale, which obviously won't move the object.

\$\endgroup\$
0
\$\begingroup\$

Your code sets transform.localScale but you want transform.localPosition

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .