Search Unity

Question Script has no Raycast Manager variable

Discussion in 'AR' started by idosebban, Jun 19, 2022.

  1. idosebban

    idosebban

    Joined:
    Jun 17, 2022
    Posts:
    1
    I am new to Unity and have no coding experience.

    I am following this AR manual: https://learn.unity.com/tutorial/placing-and-manipulating-objects-in-ar#

    I am stuck on step 13:
    Drag the SpawnableObject Prefab from the Project window into the Spawnable Prefab variable (figure 12)


    I don't have the 'Raycast Manager' and 'Spawnable Prefab' variables in my Inspector window under the Spawnable Manager (script).

    This is the code in the file:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using unityengine.xr.arfoundation;
    5.  
    6. public class SpawnableManager : MonoBehaviour
    7. {
    8.     [SerializeField]
    9.     ARRaycastManager m_RaycastManager;
    10.     List<ARRaycastHit> m_Hits = new List<ARRaycastHit>();
    11.     [SerializeField]
    12.     GameObject spawnablePrefab;
    13.    
    14.     Camera arCam;
    15.     GameObject spawnedObject;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         spawnedObject = null;
    21.         arCam = GameObject.Find("AR Camera").GetComponent<Camera>();
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.         if (Input.touchCount == 0)
    28.             return;
    29.  
    30.     RaycastHit hit;
    31.     Ray ray = arCam.ScreenPointToRay(Input.GetTouch(0).position);
    32.         if (m_RaycastManager.Raycast(Input.GetTouch(0).position, m_Hits))
    33.     {
    34.             if(Input.GetTouch(0).phase == TouchPhase.Began && spawnedObject == null)
    35.             {
    36.             if (Physics.(ray, out hit))
    37.             {
    38.  
    39.                 if (hit.collider.gameObject.tag == "Spawnable")
    40.                 {
    41.                 spawnedObject = hit.collider.gameObject;
    42.             }
    43.             else
    44.             {
    45.                 SpawnPrefab(m_Hits[0].pose.position);
    46.             }
    47.             }
    48.         }
    49.         else if(Input.GetTouch(0).phase == TouchPhase.Moved && spawnedObject != null)
    50.          {
    51.             spawnedObject.transform.position = m_Hits[0].pose.position;
    52.         }
    53.         if(Input.GetTouch(0).phase == TouchPhase.Ended)
    54.         {
    55.             spawnedObject = null;
    56.             }
    57.     }
    58.     }
    59.    
    60.     private void SpawnPrefab(Vector3 spawnPosition)
    61.     {
    62.     spawnedObject = Instantiate(spawnablePrefab, spawnPosition, Quaternion.identity);
    63.     }
    64. }
    65.  
     
  2. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,062