Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Change Interaction layer Mask With Code

Discussion in 'VR' started by Matekoty, Dec 20, 2021.

  1. Matekoty

    Matekoty

    Joined:
    Apr 12, 2020
    Posts:
    8
    Is there any way to Change Interaction layer Mask With Code?
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,251
  3. Matekoty

    Matekoty

    Joined:
    Apr 12, 2020
    Posts:
    8

    Yeah... I understand this, but how can I modify the Interaction layer Mask from another code? How can I call it?
    Sure,
    "Interaction layer Mask" = myLayer;
    but I can't refer to it.
     
  4. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,251
    Wdym from another code?
     
  5. Matekoty

    Matekoty

    Joined:
    Apr 12, 2020
    Posts:
    8
    I mean from another script. I have a pre-made XRGrabInteractable script by XR Interaction Toolkit, and I want to change the Interaction Layer mask variable in this script by my own script. But I don't know how to refer to this variable.
     
  6. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,251
  7. Matekoty

    Matekoty

    Joined:
    Apr 12, 2020
    Posts:
    8
  8. Matekoty

    Matekoty

    Joined:
    Apr 12, 2020
    Posts:
    8
  9. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,251
    GetComponent<Grabbable>.InteractionLayerMask = MyLayer

    If it's private just make it public
     
  10. Matekoty

    Matekoty

    Joined:
    Apr 12, 2020
    Posts:
    8
    Yes! That is the solution that I'm looking for, but if I write this script, nothing happen.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.XR.Interaction.Toolkit;
    public class LayerChange : MonoBehaviour
    {
    public LayerMask RightHand;
    void Update()
    {
    GetComponent<XRBaseInteractable>().interactionLayerMask = RightHand;
    }
    }

     
  11. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,251
    Doesn't it update in the inspector as well?
    If so, does the code even run?
     
  12. Matekoty

    Matekoty

    Joined:
    Apr 12, 2020
    Posts:
    8
    Yes! If I build in a Debug.Log function, it works.
     
  13. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,251
    What do you mean with the last part?
    And if it works there it should just work. Otherwise I cannot help much
     
  14. RhinocerosGamesProduction

    RhinocerosGamesProduction

    Joined:
    Dec 19, 2017
    Posts:
    4
    You should try something like this:

    interactableObject.interactionLayers = InteractionLayerMask.GetMask("Default");


    Where the interactableObject must implement the IXRInteractable interface, so for example be of type XRBaseInteractable which implements that interface by default.
    The parameter(s) in GetMask are defined interaction layers.
    But keep in mind: those interaction layers from the XR Interaction Toolkit are different from normal layers, so don't mix them up!
     
  15. ukUnityVrUser

    ukUnityVrUser

    Joined:
    Dec 10, 2015
    Posts:
    11
    Thank you so much. Spent all yesterday evening puzzling over this. Kept ending up with the traditional Layers information. It worked a treat! Just come back to it this evening and it worked.
     
  16. mariacotuna

    mariacotuna

    Joined:
    Oct 11, 2021
    Posts:
    5
    Hello, I am having a similar issue but also cannot figure it out, I am trying to swap the layermask used in order to be able to distance grab with rays, this would be possible if the layermask was set to ''Default''. It is giving me an error message 'InteractionlayerMask' could not be found (are you missing a using directive or an assembly reference). This is my code
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.XR.Interaction.Toolkit;
    3.  
    4. /// <summary>
    5. /// Set the interaction layer of an interactor
    6. /// </summary>
    7. public class SetInteractionLayer : MonoBehaviour
    8. {
    9.     [Tooltip("The layer that's switched to")]
    10.     public InteractionLayerMask targetLayer = 0;
    11.  
    12.     private XRBaseInteractor interactor = null;
    13.     private InteractionLayerMask originalLayer = 0;
    14.  
    15.     private void Awake()
    16.     {
    17.         interactor = GetComponent<XRBaseInteractor>();
    18.         originalLayer = interactor.interactionLayers;
    19.     }
    20.  
    21.     public void SetTargetLayer()
    22.     {
    23.         interactor.interactionLayes = targetLayer;
    24.     }
    25.  
    26.     public void SetOriginalLayer()
    27.     {
    28.         interactor.interactionLayers = originalLayer;
    29.     }
    30.  
    31.     public void ToggleTargetLayer(bool value)
    32.     {
    33.         if (value)
    34.         {
    35.             SetTargetLayer();
    36.         }
    37.         else
    38.         {
    39.             SetOriginalLayer();
    40.         }
    41.     }
    42.  
    43. }
    44.  
     
  17. simonpham1294

    simonpham1294

    Joined:
    Jun 7, 2023
    Posts:
    4
    So happy I came across this 6 years later. Thanks
     
    Giantox likes this.
  18. Giantox

    Giantox

    Joined:
    Aug 30, 2023
    Posts:
    1
    So glad I found it and that it is still functional code
    Took me 10 minutes to implement but now it works
    Love this community and love using Unity
     
  19. NorthernCoder

    NorthernCoder

    Joined:
    Oct 17, 2022
    Posts:
    1
    What if I want to activate/deactivate one specific Layer ?
    upload_2024-1-17_14-1-59.png

    I want to disable Grab and enable it later. However, I can't deactivate the whole since Socket has to work.
     
  20. Kuhnhero

    Kuhnhero

    Joined:
    Dec 28, 2020
    Posts:
    1
    I think you can disable a layer by using ^= instead of =. Not sure though.