Line Renderer Null Reference Exception Error in Development Build

Howdy,

Having a little trouble with a Null Reference Exception Error in my build version of the game I’m working on. Everything works fine in the IDE, but when I Build & Run I get Null Reference Errors in the following functions:

WorldUp();
WorldRight();
WorldForward();

The image below shows it working in the Game Preview, but like I said, when I Build & Run the green, red and blue lines dis spear and throw Null Error Exceptions.

I’m not sure why. I understand that the objects in those functions are saying the object doesn’t exist…but it does. I declare their existence at the beginning of the code, and then initialize them in the:

InitWorldAxisDisplay();

Function. What am I doing wrong? It’s gotta be something simple. I’ve tried adding a:

upLine = upLine.GetComponent();

to the respective functions, I’ve even created the LineRenderer Object and Component in the functions that are causing the issue, that just results in a new line being drawn every frame, which is definitely not what I want to happen. Here’s the code:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(LineRenderer))]

public class ShipVector : MonoBehaviour
{
    const int VERTICES = 10;

    public float accelInG = 0.0f;
    public float pitch = 0.0f;
    public float yaw = 0.0f;

    private Rigidbody RB = null;
    private Thruster shipThruster = null;

    //World Y Axis Line
    private LineRenderer upLine = null;
    //World X Axis Line
    private LineRenderer rightLine = null;
    //World Z Axis Line
    private LineRenderer forwardLine = null;
    //Spaceships Velocity Vector Line
    private LineRenderer vectorLine = null;

    // Use this for initialization
    void Start ()
    {
        RB = transform.root.gameObject.GetComponent<Rigidbody>();
        shipThruster = transform.root.gameObject.GetComponentInChildren<Thruster>();

        vectorLine = gameObject.GetComponent<LineRenderer>();

        vectorLine.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
        vectorLine.receiveShadows = false;
        vectorLine.useWorldSpace = true;
        vectorLine.SetWidth(5.0f, 5.0f);
        vectorLine.SetVertexCount(VERTICES);
        vectorLine.SetColors(Color.clear, Color.yellow);
        vectorLine.material = new Material(Shader.Find("Particles/Additive"));

        InitWorldAxisDisplay();
    }
	
	// Update is called once per frame
	void Update ()
    {
        yaw = RB.rotation.eulerAngles.y;
        pitch = RB.rotation.eulerAngles.x;
        transform.position = RB.position;

        accelInG = (shipThruster.Thrust / (RB.mass)) / Physics.gravity.magnitude;

        WorldUp();
        WorldRight();
        WorldForward();

        ShipsManeuverVector(-pitch, yaw);
    }

    private void ShipsManeuverVector(float Pitch, float Yaw)
    {
        RB = transform.root.gameObject.GetComponent<Rigidbody>();
        shipThruster = transform.root.gameObject.GetComponentInChildren<Thruster>();

        float thrust = shipThruster.Thrust;
        float acceleration = 0.0f;
        float accelInG = (thrust / (RB.mass)) / Physics.gravity.magnitude;

        //Obtain to the RB Center of Mass as the starting point for the Vector Line
        Vector3 Position = transform.position;

        //Assign the Position to its respective index position, using the desired acceleration, pitch and yaw
        for (int i = 0; i < VERTICES; i++)
        {
            vectorLine.SetPosition(i, Position);

            acceleration = (RB.mass * (accelInG * Physics.gravity.magnitude)) / RB.mass;
            Position += new Vector3((RB.velocity.x * i) + ((0.5f * (Mathf.Sin(Yaw * Mathf.Deg2Rad) * acceleration)) * Mathf.Pow(i, 2.0f)), (RB.velocity.y * i) + ((0.5f * (Mathf.Sin(Pitch * Mathf.Deg2Rad) * acceleration)) * Mathf.Pow(i, 2.0f)), (RB.velocity.z * i) + ((0.5f * (Mathf.Cos(Yaw * Mathf.Deg2Rad) * acceleration) * Mathf.Pow(i, 2.0f))));
        }
    }

    private void InitWorldAxisDisplay()
    {
        //Y Axis
        upLine = new GameObject("Up").AddComponent<LineRenderer>();
        //Modify the LineRenderer
        upLine.SetColors(Color.clear, Color.green);
        upLine.material = new Material(Shader.Find("Particles/Additive"));
        upLine.SetWidth(5.0f, 5.0f);
        upLine.SetVertexCount(2);

        //X Axis
        rightLine = new GameObject("Right").AddComponent<LineRenderer>();
        //Modify the LineRenderer
        rightLine.SetColors(Color.clear, Color.red);
        rightLine.material = new Material(Shader.Find("Particles/Additive"));
        rightLine.SetWidth(5.0f, 5.0f);
        rightLine.SetVertexCount(2);

        //Z Axis
        forwardLine = new GameObject("Forward").AddComponent<LineRenderer>();
        //Modify the LineRenderer
        forwardLine.SetColors(Color.clear, Color.blue);
        forwardLine.material = new Material(Shader.Find("Particles/Additive"));
        forwardLine.SetWidth(5.0f, 5.0f);
        forwardLine.SetVertexCount(2);
    }

    private void WorldUp()
    {
        if (upLine != null)
        {
            upLine.SetPosition(0, transform.position);
            upLine.SetPosition(1, transform.position + (Vector3.up * 100.0f));
        }
        else
        {
            Debug.Log("WorldUp is Null!");
        }
    }

    private void WorldRight()
    {
        if (rightLine != null)
        {
            rightLine.SetPosition(0, transform.position);
            rightLine.SetPosition(1, transform.position + (Vector3.right * 100.0f));
        }
        else
        {
            Debug.Log("WorldRight is Null!");
        }
    }

    private void WorldForward()
    {
        if (forwardLine != null)
        {
            forwardLine.SetPosition(0, transform.position);
            forwardLine.SetPosition(1, transform.position + (Vector3.forward * 100.0f));
        }
        else
        {
            Debug.Log("WorldForward is Null!");
        }
    }
}

Appreciate any and all assistance! I’ve definitely hit the end of my abilities to trouble shoot (not that they’re that extensive to begin with…heh)

V/R

Brad

http://answers.unity3d.com/questions/1079400/linerenderer-doesnt-work-in-build.html

It looks like you might not be referencing your shader.