I have this task. I need to implement two buttons, when you press them, the prefab will appear. at startup I can see that the placeable prefab field is updated, but the actual pressing of the button does not happen (I added log output to the console when the button is pressed). I don’t understand what the problem is, because I created the project through AR Core and through 3D. Nothing works. Help, please
enter image description here[enter image description here](https://i.sstatic.net/Um0ra4uE.[enter image description here](https://i.sstatic.net/3GEpWTil.png)png)enter image description here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
[RequireComponent(typeof(ARRaycastManager))]
public class NewBehaviourScript : MonoBehaviour
{
private ARRaycastManager raycastManager;
private GameObject spawnedObject;
private List<GameObject> placedPrefabsList = new List<GameObject>();
[SerializeField]
private int maxPrefabSpawnCount;
private int placedPrefabCount;
public GameObject placeablePrefab;
static List<ARRaycastHit> s_Hits = new List<ARRaycastHit>();
private void Awake()
{
raycastManager = GetComponent<ARRaycastManager>();
}
bool TryGetTouchPosition(out Vector2 touchPosition)
{
if (Input.touchCount > 0) // Проверяем, есть ли хотя бы одно касание
{
Debug.Log("Input.touchCount: " + Input.touchCount); // Выводим количество касаний
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
Debug.Log("Touch detected at position: " + Input.GetTouch(0).position);
touchPosition = Input.GetTouch(0).position;
return true;
}
else
{
Debug.Log("Touch phase is not Began: " + Input.GetTouch(0).phase);
}
}
touchPosition = default;
return false;
}
private void Update()
{
if (!TryGetTouchPosition(out Vector2 touchPosition))
{
return;
}
if (raycastManager.Raycast(touchPosition, s_Hits, TrackableType.PlaneWithinPolygon))
{
var hitPose = s_Hits[0].pose;
if (placedPrefabCount < maxPrefabSpawnCount)
{
Debug.Log("1");
SpawnPrefab(hitPose);
}
}
}
private void SpawnPrefab(Pose hitPose)
{
spawnedObject = Instantiate(placeablePrefab, hitPose.position, hitPose.rotation);
placedPrefabsList.Add(spawnedObject);
placedPrefabCount++;
}
public void SetPrefabType(GameObject prefabType)
{
placeablePrefab = prefabType;
}
}```
I'm trying to make certain prefabs appear when I click on them
acr1d _ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.