• 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 unity_gd3I5NJvhKz8CA · Jan 20 at 10:07 AM · scripting problemmovementobject

Multiple object with same script

Hi all, new here and I have small problem that I cannot solve. I tried to read the forum and couldn't find solution that will help me. Anyway to the point:

I have a script attached to object. to move the object. All good. but when I add another object, they are moving together. I tried to debug and saw that line 34 was executed twice (2 object on the display) So I was thinking can it be because of the Raycast detection in same script?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class moveBlock : MonoBehaviour
 {
 
     private RaycastHit hit;                 //Detect side of block that was presssed
     private Vector3 mousePos;               //Mouse position on the collision
     private string hitName ;                //Collider name
     private Vector2 origPos, targetPos;     //Initioate original and target positions
     private float timeToMove = 0.2f;        //Time that will take to move the block
     private bool isMoving;                  //Is the block moving
     // Update is called once per frame
     void Update()
     {
         mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         if (Physics.Raycast(mousePos, Vector3.forward, out hit, 100.0f))
         {
             hitName = hit.collider.name;        //Write the name of the collider that was hit
         }
         else
         {
             hitName = "none";                   //If no collision accured, none. 
         }
 
         //set if velocity is not zero, isMoving = true;
 
         if (Input.GetMouseButtonDown(0) && !isMoving)
         {
             switch (hitName)
             {
                 case "r":
                     StartCoroutine(MoveBlock(Vector2.right / 2));
                     break;
                 case "l":
                     StartCoroutine(MoveBlock(Vector2.left / 2));
                     break;
                 case "u":
                     StartCoroutine(MoveBlock(Vector2.up / 2));
                     break;
                 case "d":
                     StartCoroutine(MoveBlock(Vector2.down / 2));
                     break;
                 default:
                     break;
             }
         }
 
     }
 
 
     private IEnumerator MoveBlock (Vector2 direction)
     {
         isMoving = true;
         float elapsedTime = 0;
 
         origPos = transform.position;
         targetPos = origPos + direction;
 
         while(elapsedTime<timeToMove)
         {
             transform.position = Vector2.Lerp(origPos, targetPos, (elapsedTime / timeToMove));
             elapsedTime += Time.deltaTime;
             yield return null;
         }
 
         transform.position = targetPos;
 
         isMoving = false;
     }
 
 }

Any idea where can I find good explanation or maybe someone have a explanation? Thanks in advance.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by logicandchaos · Jan 20 at 03:27 PM

The move together because they have the same script on them. To make them move differently you will need to write a new script. Line 34 is called twice, once for each object. When case are is triggered for object 1 it is also triggered for object 2. How do you want the second object to move?

Comment
Add comment · Show 1 · 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 unity_gd3I5NJvhKz8CA · Jan 20 at 04:26 PM 0
Share

I don't want it to move until I click on it.

So I started to write 2 different scripts already.

One will detect a click on the block. Specific side of the block (to what side to move) and also will get name of specific block.

the other script will move block (it will get name of the block and to what side it will move)

Think I will connect both scripts to $$anonymous$$ain camera? Or is there any other way around. $$anonymous$$aybe more proper?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

252 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

manipulating position.y in unity 2 Answers

How make movement speed constant with FPS? 1 Answer

Change Script so that the object's position can be modified in the Editor/through other scripts? 1 Answer

How to Touch Drag 3D Objects 2 Answers

How do i make a mouse look script? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges