• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by overseer314 · Nov 25, 2015 at 08:52 PM · c#networklayerslayermask

A game object can only be in one layer. The layer needs to be in the range [0...31]

I am following a series of tutorials as closely as I can, but I am having a problem. I think this may be the source. When I have 2 players in the scene running in different instances of the game, one is supposed to be local, and the other is supposed to be remote. however, both stay local, and it gives me the error "A game object can only be in one layer. The layer needs to be in the range [0...31]" Here is my full script:

using UnityEngine; using UnityEngine.Networking;

public class PlayerSetup : NetworkBehaviour {

 [SerializeField]
 Behaviour[] componentsToDisable;

 [SerializeField]
 string remoteLayerName = "RemotePlayer";

 Camera sceneCamera;

 void Start()
 {
     if (!isLocalPlayer)
     {
         DisableComponents();
         AssignRemoteLayer();
     }

     else
     {
         sceneCamera = Camera.main;
         if (sceneCamera != null)
             Camera.main.gameObject.SetActive(false);
     }

     RegisterPlayer();

 }

 void RegisterPlayer()
 {
     string _ID = "Player " + GetComponent<NetworkIdentity>().netId;
     transform.name = _ID;
 }

 void AssignRemoteLayer()
 {
     gameObject.layer = LayerMask.NameToLayer(remoteLayerName);
 }

 void DisableComponents()
 {
     for (int i = 0; i < componentsToDisable.Length; i++)
     {
         componentsToDisable[i].enabled = false;
     }
 }

 void OnDisable()
 {
     if (sceneCamera != null)
         sceneCamera.gameObject.SetActive(true);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by Farwall · Aug 30, 2017 at 09:43 AM

I know its bit a late reply. But might be helpfull for others. So This error comes when Layer is missing. Goto Edit > Project Settings > Tags and Layers You need to add Layers Name which you are using.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JonTheCoder · Jan 22, 2018 at 06:55 AM 0
Share

This answer was very helpful to me. :) (unlike the error in Unity) I couldn't figure out what was going on. Was I somehow simultaneously setting my object to two layers via some crazy unseen multithreading going on?

Nope. Didn't set up my layer. :P

avatar image
1

Answer by AhmedSabry19 · Mar 20, 2016 at 02:01 PM

@overseer314 I had the same problem with brackies tutorial series :D I solved it by adding this is the Start()

 mask = LayerMask.NameToLayer("RemotePlayer") ;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Eyliess · Jun 16, 2019 at 01:55 PM

Old post, but this also happens if you try to assign a GameObject's layer to a Layer Mask, a silly mistake but it won't come up as an error in the compiler.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jun 16, 2019 at 02:58 PM 0
Share

Of course. Any time you try to assign a layer index that is smaller than 0 or greater than 31 this error will be thrown. In the case of the OP he used LayerMask.NameToLayer which returns "-1" in case the layer name doesn't exist. Assigning a layermaks does not automatically mean you get that error, only when the actual value of your mask is greater than 31. This happens when the mask contains any layer above layer 5. Any yes, assigning a layermask to the layer index property won't work. From the compiler perspective a layer index as well as a layer mask are both just integer values. So there's no way the compiler could detect this mistake.

avatar image Eyliess Bunny83 · Jun 17, 2019 at 06:57 AM 0
Share

Thanks for the clarification. I just wanted to mention it despite of what the OP's issue is because when googling the error it's the first thing that comes up.

avatar image
0

Answer by wajidali892 · Apr 16, 2018 at 11:49 AM

The other way of doing this is simple and easy By declaring a enum playerLayers {LocalPlayer=8,RemotePlayer=9} Then assigning the gameobject.layer the value as follows Gameobject.layer = (int)playerLayers.RemotePlayer The remote player value is used in your case. I hope you guys like this solution

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by MoMughal · Aug 27, 2018 at 12:38 AM

I just started watching Brackeys and got the same problem, got it fixed simply by hard coding the layer name in the function

void AssignRemoteLayer() { gameObject.layer = LayerMask.NameToLayer("RemotePlayer"); }

I guess the variable is not being set on start thus the error

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ArgumentException: NameToLayer can only be called from the main thread 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Detecting that I'm clicking a unit even though I'm not? 0 Answers

Trouble Setting/Combining LayerMask with "Builtin" Layer 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges