- Home /
Moving a single instance
I draw a line on which I want to add "handles", these "handles" can be moved and leave a line from their respective anchorpoint, to the new position of their respective "handle". So I made a prefab "handle" which gets instantiated on the line after a certain distance from the last "handle" was drawn. Each handle should be able to move independently, because I want to use the line length for other calculations. But when I have more than 1 instance, all overlap when I try to move a single "handle".
Here's some code to elaborate:
var rubberBand:GameObject;
private var uniqueRubberBand:GameObject;
private var rubberBandName:String;
uniqueRubberBand = Instantiate(rubberBand, Vector3(linepos.x, linepos.y + 5, linepos.z), transform.rotation);
rubberBandName = "RubberBand";
rubberBandName = rubberBandName.Insert(rubberBandName.Length, rubberBandNameArray.length.ToString());
Now for the line drawing, that is happening in a script attached to the prefab:
function Start ()
{
anchor = transform.position;
drag = false;
//Line1
line = new LineRenderer();
line = gameObject.AddComponent(LineRenderer);
material = new Material(Shader.Find("Diffuse"));
material.color = Color.red;
line.material = material;
line.SetVertexCount(0);
line.SetWidth(startWidth, endWidth);
//Line2
lineHelper2 = new GameObject();
lineHelper2.name = this.name + "lineHelper2";
line2 = new LineRenderer();
line2 = lineHelper2.AddComponent(LineRenderer);
line2.SetVertexCount(0);
line2.SetWidth(startWidth, endWidth);
material2 = new Material(Shader.Find("Diffuse"));
material2.color = Color.blue;
line2.material = material2;
//Line3
...
}
function Update()
{...
line.SetVertexCount(2);
line.SetPosition(0, anchor);
line.SetPosition(1, anchor + unitVector * 25);
line2.SetVertexCount(2);
line2.SetPosition(0, anchor + unitVector * 25);
line2.SetPosition(1, anchor + unitVector * 50);
line3.SetVertexCount(2);
line3.SetPosition(0, anchor + unitVector * 50);
line3.SetPosition(1, transform.position);
...}
Answer by Luci85 · Apr 05, 2012 at 06:53 PM
Just found the solution: Make a raycast and test if the hit is the instance itself and not something else.
if (Physics.Raycast (ray, hit, Mathf.Infinity))
if(hit.transform.name == this.name)
Your answer
![](https://koobas.hobune.stream/wayback/20220613065942im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Affect All Instances Of A Prefab 3 Answers
How to remove a specific instance of a prefab from List? 1 Answer
Gameobject missing camera when instance prefab 3 Answers
How can I set the material on an instance after creating it? 1 Answer
How can I link a GameObject instance to a script variable in a Prefab using the editor? 1 Answer