Getting Started with magic leap-I can’t raycast in the tutorial

5 minute read

I can’t raycast in the magic leap tutorial

For some reason, I started to touch magic leap, so I will make a note of the troubles at that time.

  • It is a memo writing at a level that I have never touched C #

What is this doing

This tutorial is for dealing with “Raycast” with magic leap. Raycast is to shoot a ray and get the information of the destination where it hits. This time, it seems that it is made to generate a cube after that.


When I was doing 2.2 Raycast in Unity, I got an error. Among them, this is the one that did not disappear even if it was corrected according to the error message.

error CS0117: GetWorldRays' does not contain a definition for MLRaycast’

The cause of this error seems to be that the instruction GetWorldRays does not exist. After all, this tutorial was made for SDK 0.22, and the SDK I’m using now is 0.24.

To solve

This is the solution that an amateur can do. I found an article written for v0.24 called Raycast Snippet –Unity, so I decided to do it myself. Interpret and proceed.

Rough flow

The basics are the same as the tutorial for 0.22.

  1. Create an empty GameObject for Raycast
  2. Create a C # script
  3. Register the script with the game object

This time I stumbled on No. 3, so I will write about it.

Specific code

  • I just copied and pasted the referenced page


using System.Collections;
using UnityEngine;
using UnityEngine.XR.MagicLeap;

public class MLRaycast : MonoBehaviour 

    public Transform camTransform;
    public GameObject prefab;

    private MLRaycast.QueryParams _raycastParams = new MLRaycast.QueryParams();

    void Start() 
        // Start raycasting.

    private void OnDestroy() 
        // Stop raycasting.
    void Update()
        // Update the orientation data in the raycast parameters.
        _raycastParams.Position = camTransform.position;
        _raycastParams.Direction = camTransform.forward;
        _raycastParams.UpVector = camTransform.up;

        // Make a raycast request using the raycast parameters 
        MLRaycast.Raycast(_raycastParams, HandleOnReceiveRaycast);

    private IEnumerator NormalMarker(Vector3 point, Vector3 normal) 
        // Rotate the prefab to match given normal.
        Quaternion rotation = Quaternion.FromToRotation(Vector3.up, normal);
        // Instantiate the prefab at the given point.
        GameObject go = Instantiate(prefab, point, rotation);
        // Wait 2 seconds then destroy the prefab.
        yield return new WaitForSeconds(2);
    void HandleOnReceiveRaycast( MLRaycast.ResultState state, 
                                UnityEngine.Vector3 point, Vector3 normal, 
                                float confidence) 
        if (state ==  MLRaycast.ResultState.HitObserved) 
            StartCoroutine(NormalMarker(point, normal));

Replace the content like this. I will read this content.

As a premise, it may be smooth if you read Series C # Introduction to Classes and Objects. To put it simply, I think that the class is the blueprint of the instruction and the object is the instruction itself created from the blueprint.

using UnityEngine.XR.MagicLeap;
It’s like adding a magicleap namespace. A namespace is a collection of classes (like statements?). It seems that there may be a smaller namespace inside the namespace, such as a folder. This makes it possible to distinguish classes with the same name by which namespace they are in.

In other words, by making this declaration, you can use statements related to magic leap.

  • By the way, System.Collections is a standard namespace for unity (like the stdio header in C?), And ʻUnityEngine` is a namespace related to unity.

public class RaycastSnippet : MonoBehavior
Declared a class called RaycastSnippet. public is a word that can be used at any time regardless of the namespace declaration. By using this, you can use this class from outside this code. In other words, you can use Raycast Snippet from the unity editor.

: MonoBehavior adds attributes for this class. MonoBehavior is an attribute that is automatically added when created with unity, and seems to be an attribute triggered by the movement of an object.

public Transfrom camTransfrom;
A class called camTranfrom is declared in the form of Tranfrom so that it can be used from the outside using public. Transfrom is a class for moving the game object, and this time it is used to move the camera. Now you are ready to use the location information of the gameobject.

public GameObject prefab;
You will be able to handle gem objects from scripts in a form that can be used from the outside using public. It seems to be used to generate cubes by raycasting.

private MLRaycast.QueryParams _raycastParams = new MLRaycast.QueryParams();
private is a word that can be used at any time, like public, regardless of namespace declaration. Contrary to public, it will be inaccessible from outside this script. This class is used to store Ray information to be sent to a class called MLRaycast.Raycast that will be used later. By adding new, it is made into an object at the same time as the declaration.

void Start()
This is the Start method. A method is a collection of multiple classes. In particular, Start is a special name and will only be executed once when this script is executed. Raycast starts with MLRaycast.Start ();.

private void OnDestroy()
This is also a method that is called at a specific timing like the Start method. This is the method that will be called when the gameobject is deleted. This time, it is set to end the raycast by MLRaycast.Stop ();.

void Update()
This is also a method that is called at a specific timing like the Start method. Update method is a method that is executed every time the screen is updated in unity. This time, it seems that the camera is moved and the input is confirmed every time.

_raycastParams.〇〇 = camTransform.〇〇;
CamTransfrom.〇〇 is substituted for _raycastPatams.〇〇 prepared in advance. In other words, the state of the main camera is always received as a parameter of ML Raycast. This will move the main camera and the rays in the same way. It is necessary to check the structure and functions of MLRaycast.QueryParams and Transfrom at unity Script Conference. There seems to be.

MLRaycast.Raycast(_raycastParams, HandleOnReceiveRaycast);
This is the class that actually runs Raycast. It receives the parameters of the ray and the action when it collides with an object as arguments.

private IEnumerator NormalMarker(Vector3 point, Vector3 normal)
A class for creating a cube when a ray hits an object.


  1. Series C # Introduction to Classes and Objects
  2. [For super beginners] I’m finally convinced, the answer to C # that comes out when I touch Unity for the first time
  3. Raycast Summary
  4. [Unity] Change transform.position to move the object!
  5. [Magic Leap] Get the detected plane with Ray and place the object vertically
  6. Do not run OnDisable or OnDestroy when the editor is stopped [Unity]