Problems about Line Renderer

Hey, folks, there seems a small weird problems about LineRenderer, each time you generated a new mesh with Line Renderer, the new mesh always appears at the original point(0,0,0)first, and then draw to the desired position. This could be a very tiny problem for most of projects, but i just feel very wired why the mesh doesnt be rendered to the required position directly.

The following are the sripts for you to test the problem.

script1:

public var LR: LineRenderer;
private var p1 = Vector3(5,5,5);
private var p2 = Vector3(10,10,10);

function Update ()
{
  LR.SetVertexCount(2);
  LR.SetPosition(0, p1);
  LR.SetPosition(1, p2);


}

@script RequireComponent(LineRenderer)

script2

public var go: GameObject;

function Update () {


if(Input.GetButton("Fire2"))

Instantiate(go, transform.position, transform.rotation);

}

creat an empty GameObject, add LineRenderer Component and then assign a material to it. Attach script to this gameObject make it to a prefab.

Create another empty GameObject, attach scipt2 to it, assign the previous made prefab to it.

Ok, start the game, click the right button of mouse, you will see the instantiated Line Mesh appears at the world original point first, and then jump the position required, which is really weird.

[/img]

(as it shows in the attached pic, the first frame after the Line Mesh being instantiated, it appears at the original point)

201596--7437--$pic1_336.png

The problem is that Instantiate() is called in one frame, and the object is instantiated, but it is only updated the next frame. One solution would be to have the Instantiating script call Update on the LineRenderer object’s script right after instantiation:

var instance = Instantiate(go, transform.position, transform.rotation);
instance.GetComponent(Script1).Update();

(Your script is probably called something other than Script1, though)

  1. made a stupid mistake, actually we can solve this problem very easily.

In the Start() function, set LineRenderer.enabled = false, and in the Update() function, set LineRenderer.enabled = true.

Then when the new mesh with LineRenderer is instantiated, it will not be rendered at the first frame.