Creating a GameObject between two

As shown in the image below, I have 5 game object placed on the screen(There can be many more)And this objects are instantiated at runtime when certain events occurs,since this elements are instantiated at runtime they could be placed randomly and each sphere can be dragged as well.Now what I want is when user touch any two objects one after the other a line should connect them (rather cylinder/capsule in unity) for example if user touches sphere1 and sphere2 one after the other then Sphere1 and Sphere2 should be connected.And once any these objects are connected,they cant be connected again.I just tried with two spheres and that too with fixed distance between them and no dragging as well.So can any one suggest how to go about it.


To calculate the distance between the spheres, you can use Vector3.distance.

But what you want is a line, not a cylinder I suspect. You can use the linerenderer, or invest a few bucks in the excellent line drawing library Vectrosity.

There are a number of different technical problems in your question. You are going to have to break it apart and figure out how to solve each.

  • First you are going to have to figure out how to detect the object you click on. The best way for your game is going to be Raycasting()… There are a bizillion questions and answers in Unity Answers on Raycasting(), plus see the reference page to Physics.Raycast().
  • You are going to have to distinguish between a click and a drag, since you say a user can drag the spheres. See the Input class and the OnMouse*() functions.
  • You are likely going to have to communicate to scripts on the spheres to mark and clear selections, plus also to mark the sphere as ‘taken’ when a line is placed. See Accessing Other Game Objects.
  • Then you are going to need to position a line or a the cylinder. If you just want to use a line, Vectrosity from the Asset store is a great package. If you want to go the cylinder route, I’ve posted several answers on how to place, rotate, and scale object to fit between to other positions. You can find one post here.

Note Unity Answers is not designed for this kind of discussion with multiple technical questions. Unity Forums is a better fit. At UA we try to restrict questions to a single technical issue.

Thanks @robertbu i got started with something.atleast i am now able to create a cylinder between two objects.Will look for algorithm for determining how many GameObjects are there on the screen.Here is the code which i tried.May be you can point out a bit more with it.

function Update () 
   		            if (Input.GetMouseButtonDown(0))
           			if (Physics.Raycast (ray, hit)) 
        				sphereIndex = testscript.sphereCollider.IndexOf ( hit.collider );
        				if ( sphereIndex!= -1 )
        					else if(touchCount==2)
                                       if (pos2 != pos1) 
                       			            cylinderbond= Instantiate ( singleBond,previousposition,Quaternion.identity );
        									v3 = previousposition-currentposition; 
        									cylinderbond.transform.position = currentposition + (v3) / 2.0;
        									cylinderbond.transform.localScale.x = v3.magnitude/4.0;
        									cylinderbond.transform.rotation = Quaternion.FromToRotation(Vector3.right, v3);