Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Time.toMoveOn · Nov 29, 2012 at 09:24 PM · lerptranslateonmousedown

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThePunisher · Nov 29, 2012 at 10:43 PM 0
Share

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".

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Time.toMoveOn · Dec 07, 2012 at 09:33 AM

Thanks I got it now.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges