Array is out of range?

Hi!

I started coding recently, and I’m working on a dog that go to a bone when I’m throwing it. Everything is working fine when I’m playing the scene, but there’s this “Array is out of range” error in the console going on each frame, and wont stop until I throw a bone. How I’m supposed to fix this? There is my code :

public Vector3 target;
NavMeshAgent agent;
public GameObject firstBone;
public GameObject [] checkBones;

// Use this for initialization
void Start () {

	agent = GetComponent<NavMeshAgent> ();
	target = gameObject.transform.position;

}

// Update is called once per frame
void Update () {

	checkBones = GameObject.FindGameObjectsWithTag ("Os");
	firstBone = checkBones [0];

	if (checkBones != null) {

		target = firstBone.transform.position;
		agent.SetDestination (target);
	}	
}

I get this error in the console : IndexOutOfRangeException: Array index is out of range.
GoToObject.Update () (at Assets/Scripts/GoToObject.cs:28)

It helps if you paste the error in your question. I’m going to guess that checkBones is missing when you try to set firstBone. So move firstBone into the “checkBones is not null” if-statement.

 checkBones = GameObject.FindGameObjectsWithTag ("Os");
 if (checkBones != null) {
     firstBone = checkBones [0];