- Home /
Question by
Prashant_Tomar · Apr 15, 2021 at 10:46 AM ·
development build
"UnityException: Transform child out of bounds" - I am getting this error always whenever I am trying to run the code. Please help !!!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.XR.ARFoundation; using UnityEngine.XR.ARSubsystems;
public class Placer : MonoBehaviour { [Header("AR")] [SerializeField] ARRaycastManager raycastManager; [SerializeField] GameObject[] blindsPrefab; [SerializeField] Camera raycastCamera; private GameObject visual;
private List<ARRaycastHit> hits = new List<ARRaycastHit>();
// Start is called before the first frame update
void Start()
{
raycastManager = FindObjectOfType<ARRaycastManager>();
visual = transform.GetChild(0).gameObject;
visual.SetActive(false);
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0) && !IsClickOverUI())
{
Ray ray = raycastCamera.ScreenPointToRay(Input.mousePosition);
if (raycastManager.Raycast(ray, hits, TrackableType.Planes))
{
Pose pose = hits[0].pose;
foreach (GameObject gameObject in blindsPrefab)
{
if (FindObjectOfType<ToggleAR>().isOn)
{
gameObject.transform.SetPositionAndRotation(pose.position, pose.rotation);
if (!visual.activeInHierarchy)
visual.SetActive(true);
}
}
}
}
}
bool IsClickOverUI()
{
PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
pointerEventData.position = Input.mousePosition;
List<RaycastResult> raycastResults = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerEventData, raycastResults);
for (int i = 0; i < raycastResults.Count; i++)
{
if (raycastResults[i].gameObject.GetComponent<MouseOverClickThrough>() != null)
{
raycastResults.RemoveAt(i);
i--;
}
}
return raycastResults.Count > 0;
}
}
Comment
Answer by michaelfelleisen · Apr 15, 2021 at 11:11 AM
I like to iterate backwards through lists if I am removing elements.
for(int i = raycastResult.Count; i >= 0; i--) {
//stuff
}