I have two space ships , both are moving in space, I want to create beam like effect between these ships, but I get error (NullReferenceException: Object reference not set to an instance of an object) And I have Transform - laser beam prefab that I want to use as beam, this (everything) works well if this is on script attached to ship, but I need to create them in runtime (I cant have prepared objects on scene, or track them all, just want to create laser, give him 2 objects between whose it will resonate and script on this laser beam will destroy itself after certain time)
So what am I doing wrong , how can I give Script on Laser beam(line renderer) two references on objects to track their positions properly?
//code creating/triggering laser
actual_laserBeam = ((Transform) Instantiate(laserBeamPrefab, _pos, direction_with_inaccurency)).GetComponent<LineRenderer>();
actual_laserBeam.SetWidth(10,10);
_LaserBeam my = actual_laserBeam.GetComponent<_LaserBeam>();
my.start = new GameObject(); //here comes aforementioned error
my.end = new GameObject(); //here comes aforementioned error
//new GameObject is used only as an example, because error happends always
//and...
using UnityEngine;
using System.Collections;
public class _LaserBeam : MonoBehaviour {
public GameObject start;
public GameObject end;
private LineRenderer lineRenderer;
// Use this for initialization
public void nastav(GameObject _start, GameObject _end) {
start = _start;
end = _end;
}
void Start () {
lineRenderer = GetComponent<LineRenderer>();
lineRenderer.SetWidth(10,10);
Destroy(this,3);
}
// Update is called once per frame
void Update () {
lineRenderer.SetPosition(0,start.transform.position);
lineRenderer.SetPosition(1,end.transform.position);
}
}