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 _= pipes*.transform.position;*_
bottomTransforms = pipes*.transform.GetChild(0);*
topTransforms = pipes*.transform.GetChild(1);*
}
* }*
* void Update ()*
{
}
}
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 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 Positions = new List();*
-
[HideInInspector] public List TopTransforms = new List();*
-
[HideInInspector] public List BottomTransforms = new List();*
-
void Start()*
-
{*
-
GameObject[] pipes = GameObject.FindGameObjectsWithTag ("PipeHolder");*
-
foreach (GameObject pipe in pipes)*
-
{*
-
Positions.Add (pipe.transform.position);*
if( pipe.transform.childCount >= 1)
if( pipe.transform.childCount >= 2)