- Home /
Array of transform index out of range error
What is wrong in here? This is the second time I want to ask this because the first time it just started to work and I didn't need to ask anymore. But now, again it doesn't work and I can't figure out what it is. I get index out of range error and the array is empty whilst moments ago it worked just fine and got all the objects in the arrays.
using UnityEngine; using System.Collections;
public class PipeTopBottomDistance : MonoBehaviour { public GameObject[] pipes = new GameObject[8]; public Vector3[] positions;
public Transform[] bottomTransforms = new Transform[8];
public Transform[] topTransforms = new Transform[8];
public float verticalPipeDistance = 3.08f;
public float pipePressOn = 3.08f, pipePressOff;
public float pressSpeed;
void Start ()
{
pipes = GameObject.FindGameObjectsWithTag("PipeHolder");
for(int i = 0; i < 8; i++)
{
positions[i] = pipes[i].transform.position;
bottomTransforms[i] = pipes[i].transform.GetChild(0);
topTransforms[i] = pipes[i].transform.GetChild(1);
}
}
void Update ()
{
}
}
Now after playing a bit with the script.. like commenting out some lines to see if I can isolate the problem.. or deleting the "new Transforms[8]" when declaring the transforms and still failing to work.. it now works again and I brought the code back to be literally exactly the same as the one I pasted here and it works.. what am I missing? I fear it will happen again and I want to know exactly what the problem is..
And now I just copied and pasted the script I put here to be sure I'm not missing anything and it still works... Why it didn't work earlier.. what would the explanation be?
Answer by FlyingHighUp · Dec 08, 2015 at 08:39 PM
It could be from multiple places here.
The for loop you have requires there be >= 8 GameObjects in your scene with the tag "PipeHolder". If GameObject.FindGameObjectsWithTag finds less than 8, pipes[i] will throw a exception.
You'll also get the exception if positions[], bottomTransforms[] or topTransforms[] have less than 8 elements. Also, because they're being serialized to the inspector - you need to make sure they have 8 elements in the inspector.
In addition to this, each found pipe Gameobject needs to have two children. If not you'll get a Unity exception.
I'm not exactly sure what you're trying to accomplish with your script, but personally I would write
using System.Collections.Generic;
public class YourClass : MonoBehaviour
{
[HideInInspector] public List<Vector3> Positions = new List<Vector3>();
[HideInInspector] public List<Transform> TopTransforms = new List<Transform>();
[HideInInspector] public List<Transform> BottomTransforms = new List<Transform>();
void Start()
{
GameObject[] pipes = GameObject.FindGameObjectsWithTag ("PipeHolder");
foreach (GameObject pipe in pipes)
{
Positions.Add (pipe.transform.position);
if( pipe.transform.childCount >= 1)
TopTransforms.Add (pipe.transform.GetChild (0));
if( pipe.transform.childCount >= 2)
BottomTransforms.Add (pipe.transform.GetChild (1));
}
}
}
This version works with any number of pipes, and any number of Pipe children.
Your answer
Follow this Question
Related Questions
How do I check a List[0], when it's nothing? 1 Answer
"Array index is out of range" for any index value, when array.length>index 2 Answers
How do I check the distance between multiples objects in an array from the player? 4 Answers
Changing the material of all children, then change back to old one 1 Answer
Find index number in an Array of a transform by it's name 1 Answer