Home > Back-end >  List<T> keep throwing "NullReferenceExceprion" when scripting in Unity, even though
List<T> keep throwing "NullReferenceExceprion" when scripting in Unity, even though

Time:04-16

I am doing my Unity project, and this happened when I write the C# script. It keeps throwing NullReferenceException whenever the object this script attached active. It is the List<T>.Add() function that cause that exception, but I have no solution to stop this. Notice that I've initialized the list and it seems that it didn't work properly. (What I need is a solution to this, other things can be later.)

Here is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tracer : MonoBehaviour
{
    public float tracerSize;
    public int fadeCount;

    private MeshFilter mf;
    private Mesh mesh;
    private List<Vector3> positionArray;
    private List<int> index;
    private List<Color> clr;
    private List<Vector3> vec;

    private Transform trans;

    private Color tracerColor;

    void Start()
    {
        trans = GetComponentInParent<Transform>();
        tracerColor = GetComponentInParent<SpriteRenderer>().color;
        mf = GetComponent<MeshFilter>();

        index = new List<int>() { 0, 0, 0, 0, 0, 0 };
        clr = new List<Color>() { };
        vec = new List<Vector3>() { };
        mesh = new Mesh();
        mf.mesh = mesh;
    }

    
    void Update()
    {
        positionArray.Add(trans.position);
        if (positionArray.Count > fadeCount)
        {
            positionArray.RemoveAt(0);
        }
        
        mesh.Clear();
        index.Clear();
        clr.Clear();
        vec.Clear();
        
        for (int i = 1; i < fadeCount; i  )
        {
            int ii = (i - 1) * 4;
            index.AddRange(new int[] { 0   ii, 1   ii, 2   ii, 2   ii, 3   ii, 0   ii }); // throw!
            clr.Add(new Color(tracerColor.r, tracerColor.g, tracerColor.b, (tracerSize - i) / (float)tracerSize)); // throw!
            vec.AddRange(CalculateQuad(positionArray[i - 1], positionArray[i], (tracerSize - i   1) / (float)tracerSize, (tracerSize - i) / (float)tracerSize)); // throw!
        }
        
        mesh.triangles = index.ToArray();
        mesh.colors = clr.ToArray();
        mesh.vertices = vec.ToArray();
    }

    private Vector3[] CalculateQuad(Vector3 f, Vector3 b, float sizf, float sizb)
    {
        Vector3 vec = f - b;
        Vector3 vecN = Vector3.Normalize(vec);
        Vector3 vecf = new Vector3(vecN.y, -vecN.x) * sizf, vecb = new Vector3(vecN.y, -vecN.x) * sizb;
        return new Vector3[]
        {
            vecf, -vecf, -vecb, vecb
        };
    }
}

CodePudding user response:

I guess the NullReferenceException happens on the line

positionArray.Add(trans.position);

This variable is declared but not initialized in your Start() function like your other lists

To debug this kind of issues check the error logs, it should give you the line. You could use a Debug.Log to print your variables and find the null one.

  • Related