Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can't change objects layer through script

Discussion in 'Scripting' started by Archiboi, May 15, 2022.

  1. Archiboi

    Archiboi

    Joined:
    Jan 27, 2022
    Posts:
    4
    I'm currently working on a pickup mechanic for my fps game, but when I test it in play mode, I get an error:
    A game object can only be in one layer. The layer needs to be in the range [0...31]
    UnityEngine.StackTraceUtility:ExtractStackTrace ().

    My guess is that the unity engine thinks I'm trying to set the gun to multiple layers at a time.

    Here is my code:

    Code (CSharp):
    1.  
    2. [SerializeField] Camera cam;
    3.     [SerializeField] Transform weaponPos;
    4.     [SerializeField] LayerMask InteractableLayer;
    5.     [SerializeField] LayerMask weaponLayer;
    6.  
    7.     public float grabDistance;
    8.     public float throwForce = 6f;
    9.  
    10.     GameObject weapon;
    11.  
    12.     Rigidbody rb;
    13.  
    14.     void Update()
    15.     {
    16.         if (Input.GetKeyDown(KeyCode.E))
    17.         {
    18.             RaycastHit hit;
    19.             if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, grabDistance, InteractableLayer))
    20.             {
    21.                 rb = hit.transform.GetComponent<Rigidbody>();
    22.  
    23.                 if (rb != null)
    24.                 {
    25.                     rb.useGravity = false;
    26.                     weapon = hit.transform.gameObject;
    27.                     currentLayer = weaponLayer;
    28.                     weapon.transform.position = weaponPos.position;
    29.                     weapon.transform.parent = weaponPos.transform;
    30.                 }
    31.             }
    32.         }
    33.         else if (Input.GetKeyDown(KeyCode.Q))
    34.         {
    35.             if (weapon != null)
    36.             {
    37.                 rb.useGravity = true;
    38.                 rb.AddForce(cam.transform.forward * throwForce, ForceMode.Impulse);
    39.                 currentLayer = InteractableLayer;
    40.                 weapon.transform.parent = null;
    41.                 rb = null;
    42.                 weapon = null;
    43.             }
    44.         }
    45.     }


    Is there any way I might be able to fix this?

     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
  3. Archiboi

    Archiboi

    Joined:
    Jan 27, 2022
    Posts:
    4
    This helped so much!
    Thanks heaps!
     
  4. kader1081

    kader1081

    Joined:
    Oct 16, 2021
    Posts:
    365
    probably you are trying to acces layer is not exist.Or the codes you are trying to change layers executed same time.Try to delete one of them then try again if it works then codes get executed same time

    Code (CSharp):
    1.  gameObject.layer = LayerMask.NameToLayer("thelayeryouwanttochange");
    2.  
    3. try this maybe it will work.