- Home /
Move an object by clicking on another
My aim as begginer is to understand how works the interconnections between gameObjects.
I'd like to start by translating a gameObject (named "Object2") by clicking on another gameObject ("Object1").
For the exemple I want to translate 2 meters up during 2 seconds then stop.
I need a clear answer explaining where to put the scripts and how to connect one object to another.
Thanks
@asked by Time.toMoveOn
Have you visited the script reference Unity page? You will need to be familiar with a few components before you can even begin to understand interaction between scripts or GameObjects.
It would be better if you attempted to do just what you mentioned, and then came back and said "I'm stuck with this specific part".
Answer by ThePunisher · Nov 29, 2012 at 11:15 PM
In any case, I like to code, so here it is.
A simple way you get an object to become click-able is to make sure your object has a collider and it implements one of the OnMouse methods (Like OnMouseUp, OnMouseDown). Take the following script for example:
using UnityEngine;
using System.Collections;
public class ClickableObject : MonoBehaviour
{
void OnMouseUp()
{
Debug.Log(string.Format("Object {0} was clicked.", name));
}
}
If you attach this script to an object (like a Unity sphere) and it has a collider, when that object is clicked you will see a message in your console come up.
Now we can take this functionality and add to it so that it interacts with another object. There are multiple different ways to get access to another GameObject/script, each with pros and cons, and it will be up to you to decide which one suits your needs.
One simple way to get a reference to a GameObject is to create a public member on a script of type GameObject. This will cause the Inspector (when the object with the script attached to it is selected) to give you a public property that can be modified within the editor. Now, you can take a separate GameObject from within your scene and drag it onto this new property we have created. This will give you access to that specific object you have dragged.
Let's take the previous script and modify it so that it does what we just talked about. The script is now diamonds! I mean, the script now looks like this:
using UnityEngine;
using System.Collections;
public class ClickableObject : MonoBehaviour
{
//Here is our new inspector property. To initialize this, we drag a GameObject from our scene into the property.
public GameObject m_draggedObject;
void OnMouseUp()
{
Debug.Log(string.Format("Object {0} was clicked.", name));
//Let's make sure we actually dragged something to initiliaze this member.
if (m_draggedObject != null)
{
Debug.Log(string.Format("Name of the GameObject we are referecing is {0}.", m_draggedObject.name));
}
}
}
Provided you dragged a GameObject into the property, you will now see two debug statements in your console window. One is the original message we were previously printing, and the other is the Name of the GameObject you are now referencing.
Ok, let me know if you understand this so far and we can continue with the next part.
Answer by Time.toMoveOn · Dec 04, 2012 at 10:38 AM
I could understand even if I'm totally coding with JavaScript, never tried C# yet. Anyway thanks for the answer, it's very well explained and I will need it.
Your answer

Follow this Question
Related Questions
I'm using TRANSLATE but want to use LERP. C# 2 Answers
Vector2.Lerp not working properly. Horizontal jittering when not moving horizontally at all! 1 Answer
Transform translate 1 Answer
How do I make this lerp? 1 Answer
Is Lerp acting weird? 2 Answers