Problem updating line renderer's start and end points positions at runtime

I’m trying to connect two GameObject (sphereObject0 and sphereObject2) with a line renderer lineRenderer0_2.

It shows up fine at first. My problem is that I need the line renderer to update its start and end positions to match the positions of sphereObject0 and sphereObject2.

For some reasons, my line renderer is static, and I can’t find the way to make them move together with the spheres.

Here’s my C# script, any idea about what I am doing wrong? Any help is greatly apreciated!

using UnityEngine;
using System.Collections;

public class autoCreateNetwork : MonoBehaviour {

	public bool sphereHasGravity = false;
	private Collider coll;
	private Renderer rend;
	private SpringJoint springJoint;
	public Material lines;

	GameObject sphereObject0;
	GameObject sphereObject2;
	LineRenderer lineRenderer0_2 ;

	void Start () {

		// Spawn a sphere and set it up
		sphereObject0 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
		sphereObject0.transform.position = new Vector3(-10.2571400951f, 98.8977277884f, -19.9870244083f);
		Rigidbody sphereBody0 = sphereObject0.AddComponent<Rigidbody>();
		sphereBody0.mass = 1f;
		// Physics model
		coll = sphereBody0.GetComponent<Collider> ();
		PhysicMaterial material0 = new PhysicMaterial();
		material0.dynamicFriction = 1;
		coll.material = material0;
		coll.material.bounciness = .8f;
		// Render (color, smoothness, etc.)
		rend = sphereBody0.GetComponent<Renderer>();
		rend.material.color = new Color32(171,166,164,255);
		rend.material.SetFloat("_Metallic", 0.0f);
		rend.material.SetFloat("_Glossiness", 0.0f);
		sphereBody0.useGravity = sphereHasGravity;
		sphereBody0.transform.localScale = new Vector3(0.5f,0.5f,0.5f);

		// Spawn a sphere and set it up
		sphereObject2 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
		sphereObject2.transform.position = new Vector3(-28.1943570987f, 89.5665878403f, 5.43686642264f);
		Rigidbody sphereBody2 = sphereObject2.AddComponent<Rigidbody>();
		sphereBody2.mass = 1f;
		// Physics model
		coll = sphereBody2.GetComponent<Collider> ();
		PhysicMaterial material2 = new PhysicMaterial();
		material2.dynamicFriction = 1;
		coll.material = material2;
		coll.material.bounciness = .8f;
		// Render (color, smoothness, etc.)
		rend = sphereBody2.GetComponent<Renderer>();
		rend.material.color = new Color32(105,150,187,255);
		rend.material.SetFloat("_Metallic", 0.0f);
		rend.material.SetFloat("_Glossiness", 0.0f);
		sphereBody2.useGravity = sphereHasGravity;
		sphereBody2.transform.localScale = new Vector3(0.5f,0.5f,0.5f);

		// Spawn a line and set it up
		GameObject line0_2 = new GameObject();
		LineRenderer lineRenderer0_2 = line0_2.AddComponent<LineRenderer>();
		lineRenderer0_2.material = lines;
		lineRenderer0_2.widthMultiplier = 0.0482773661501f;
		lineRenderer0_2.SetPosition(0, sphereObject0.transform.position);
		lineRenderer0_2.SetPosition(1, sphereObject2.transform.position);
		float alpha0_2 = .9f;
		Gradient gradient0_2 = new Gradient();
		gradient0_2.SetKeys(
			new GradientColorKey[] { new GradientColorKey(new Color32(171,166,164,255), 0.0f), new GradientColorKey(new Color32(105,150,187,255), 1.0f) },
			new GradientAlphaKey[] { new GradientAlphaKey(alpha0_2, 0.0f), new GradientAlphaKey(alpha0_2, 1.0f) }
		);
		lineRenderer0_2.colorGradient = gradient0_2;
		lineRenderer0_2.useWorldSpace = true;

	}

	void Update () {

			lineRenderer0_2.SetPosition(0, sphereObject0.transform.position);
			lineRenderer0_2.SetPosition(1, sphereObject2.transform.position);

		}
	}

@rodolpheg You have incorrect syntax on this line:

LineRenderer lineRenderer0_2 = line0_2.AddComponent<LineRenderer>();

You’re declaring a variable in local scope that has class scope, and because of this, your local instance of lineRenderer0_2 goes null when the Start() callback returns control to Unity. Simply remove the declaration, keep the assignment, and your script will work.