"Object at index 0 is null"

Hi, I’m making a tower defense, and when i want to draw the line to the enemy, I ve this eror : “Object at index 0 is null”.
The fact is, my program is working, but I’ve this error when i launch it.
Thanks for the answer

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

public class Turret : MonoBehaviour {

    [Header("Finding the closest enemy")]
    public Transform target;

    [Header("Values")]
    public float range = 15f;
    public string enemyTag = "Enemy";

    [Header("LineRenderer")]
    private LineRenderer line;
    public Transform origin;
    public float lineDrawSpeed = 1f;
    private float counter;
    private float dist;

	void Start () {
        line = GetComponent<LineRenderer>();
        line.SetPosition(0, origin.position);
        line.SetWidth(0.45f, 0.45f);

        InvokeRepeating("UpdateTarget", 0f, 0.2f);
	}

    void UpdateTarget()
    {
        GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
        float shortestDistance = Mathf.Infinity;
        GameObject nearestEnemy = null;

        foreach(GameObject enemy in enemies)
        {
            float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
            if(distanceToEnemy < shortestDistance)
            {
                shortestDistance = distanceToEnemy;
                nearestEnemy = enemy;
            }
        }

        if(nearestEnemy !=null && shortestDistance <= range)
        {
            target = nearestEnemy.transform;
        }
        else
        {
            target = null;
        }


    }
	
	void Update () {
        if (target == null)
        {
            
            counter = dist = 0;
            line.enabled = false;
        }
        else
        {
            line.enabled = true;
            dist = Vector3.Distance(origin.position, target.position);
            Vector3 pointA = origin.position;
            Vector3 pointB = target.position;
            if (counter < dist)
            {
                counter += 0.1f / lineDrawSpeed;
            }
            float l = Mathf.Lerp(0, dist, counter);
            Vector3 pointAlongLine = l * Vector3.Normalize(pointB - pointA) + pointA;
            line.SetPosition(1, pointAlongLine);

        }

	}
    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, range);
    }
}

ArgumentException: Object at index 0 is null
UnityEditor.SerializedObject..ctor (UnityEngine.Object[] objs, UnityEngine.Object context) (at C:/buildslave/unity/build/Editor/Mono/SerializedObject.bindings.cs:39)
UnityEditor.Editor.GetSerializedObjectInternal () (at C:/buildslave/unity/build/Editor/Mono/Inspector/Editor.cs:553)
UnityEditor.Editor.get_serializedObject () (at C:/buildslave/unity/build/Editor/Mono/Inspector/Editor.cs:452)
UnityEditor.UIElements.InspectorElement..ctor (UnityEditor.Editor editor, Mode mode) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorElement.cs:133)
UnityEditor.UIElements.EditorElement.Init () (at C:/buildslave/unity/build/Editor/Mono/Inspector/EditorElement.cs:84)
UnityEditor.UIElements.EditorElement..ctor (Int32 editorIndex, UnityEditor.InspectorWindow iw) (at C:/buildslave/unity/build/Editor/Mono/Inspector/EditorElement.cs:62)
UnityEditor.InspectorWindow.DrawEditors (UnityEditor.Editor[] editors) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:1392)
UnityEditor.InspectorWindow.RebuildContentsContainers () (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:639)
UnityEditor.InspectorWindow.RedrawFromNative () (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:311)

You can remove this error by closing and again adding the Inspector window.

I got this error because I had two inspectors open on two different items both with scripts. I hope this helps.

these kind of errors are mostly related to order of execution.

Can confirm it’s an inspector error - unity 2019.1.3f1

kept getting this error at runtime when using an object pool - but only if one of my game objects containing an object pool was selected in the inspector. (i have a script for spawnwaves on the same gameobject so it’s selected a lot)

if i chose another object in the inspector the error would disappear - which i only realised when i checked this thread - as it hadn’t been having any impedance on my project, it was just kinda annoying.

hope this helps.

I never had an error like this. However a possible cause could be this:

[Header("LineRenderer")]
private LineRenderer line;

Specifically the Header attribute that is attached to a private / non serialized field. This could cause issues when the inspector renders your fields as the field the header belongs to doesn’t exist in the serialized data. Though this is just a guess.

To be honest this was a long time ago. I was doing an enter the gungeon like which is really cool now.
I think it was an inspector error, also i used a timer of approximately 0.2f s and it was working without issues, even tho that’s just a message!

This error occurs if you have open a second inspector.

Seems like this error is in regards to the Unity Editor - after trying to open/close the Inspector window (which worked once, but didn’t work again), I literally just:

Restarted Unity and it worked!

If you only have one Inspector tab open and restarting Unity doesn’t work for you:
Change your layout to default, then change it back. My second inspector was hidden off screen since I switched from multiple monitors, thus, restarting unity didn’t work for me.