I have encountered this prolem while trying to make a pickup script for a game i am currently working on. everything seems to break because of the while on line 14, but when I change it to “if” i don’t get the desired results, I reprogrammed this, but I still am curious why a crash occours. (Unity-2021-3.33f1 LTS)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUp : MonoBehaviour
{
public Transform cameraPos;
public LayerMask mask;
public float maxDistance = 5f;
public void Update()
{
while(Input.GetMouseButtonDown(0))
CheckForObjects();
}
public void CheckForObjects()
{
Ray ray = new Ray(cameraPos.position, transform.forward);
if(Physics.Raycast(ray, out RaycastHit hit, maxDistance, mask))
{
Debug.Log(hit.collider.name + " was hit");
hit.collider.transform.position = transform.position;
}else
{
Debug.Log("Nothing was hit");
}
}
}