- Home /
"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)
I'm getting nearly the same error. I pasted my error message here and bolded the differences. I started seeing this issue when upgrading to the new 2019 build. It seems when making changes to the game using the editor causes this error to appear when trying to run the game. Restarting Unity fixes it until I make another change. I haven't found a solution yet.
ArgumentException: Object at index 0 is null UnityEditor.SerializedObject..ctor (UnityEngine.Object[] objs, UnityEngine.Object context) (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/SerializedObject.bindings.cs:39) UnityEditor.Editor.GetSerializedObjectInternal () (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/Inspector/Editor.cs:553) UnityEditor.Editor.get_serializedObject () (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/Inspector/Editor.cs:452) UnityEditor.UIElements.InspectorElement..ctor (UnityEditor.Editor editor, UnityEditor.UIElements.InspectorElement+$$anonymous$$ode mode) (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/Inspector/InspectorElement.cs:133) UnityEditor.UIElements.EditorElement.Init () (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/Inspector/EditorElement.cs:84) UnityEditor.UIElements.EditorElement..ctor (System.Int32 editorIndex, UnityEditor.InspectorWindow iw) (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/Inspector/EditorElement.cs:62) UnityEditor.InspectorWindow.DrawEditors (UnityEditor.Editor[] editors) (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/Inspector/InspectorWindow.cs:1392) UnityEditor.InspectorWindow.RebuildContentsContainers () (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/Inspector/InspectorWindow.cs:639) UnityEditor.InspectorWindow.RedrawFromNative () (at C:/buildslave/unity/build/Editor/$$anonymous$$ono/Inspector/InspectorWindow.cs:311)
Answer by gaggedegg · May 30, 2019 at 09:33 AM
You can remove this error by closing and again adding the Inspector window.
Worked for me. Seems there is a bug with opening a second inspector, then closing the first causes index 0 to be null but index 1 (the second inspector open) not. Unity can fix this easily... I feel let down.
Answer by elijahjns · May 18, 2019 at 06:43 PM
I got this error because I had two inspectors open on two different items both with scripts. I hope this helps.
Actually it's happening if I start the game with any scripted object selected. What an odd error.
Looks like you are right. I have been having the same issue, and here I was restarting Unity to make it go away. But CTRL+clicking to deselect the object before running the game does keep the error from appearing.
I'm still wondering if there is something within our projects that is causing this or is it totally Unity's fault. It seems that not a whole lot of people are complaining about it.
Answer by tylerwongj · Oct 12, 2019 at 12:24 AM
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!
Answer by sp3cim4n_ · Apr 21, 2019 at 12:07 PM
these kind of errors are mostly related to order of execution.
Answer by MrSmokes · May 23, 2019 at 03:05 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612195915im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Array error - Index is less than 0... 3 Answers
Null first array index and shift rest by 1? 1 Answer
how can I check if an object is null? 4 Answers
what is null 1 Answer