Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 numberkruncher · Feb 29, 2012 at 01:33 AM · c#layersstaticlayermaskargumentexception

ArgumentException: NameToLayer can only be called from the main thread

I was hoping to cache the response of the NameToLayer function as I suspect it will offer better performance (even if only minimal). Whilst the following source does in fact work, I would like to avoid the warning/error.

 public class MyObject : MonoBehaviour {
 
     public static readonly int LAYER_PROJECTILE;

     static MyObject() {
         LAYER_PROJECTILE = LayerMask.NameToLayer("Projectile");
     }

     ...

     void OnTriggerEnter(Collider collider) {
         if (collider.gameObject.layer == LAYER_PROJECTILE) {
             ...
         }
         else { ... }
     }

 }

Is there a better approach to this (that doesn't involve storing the layer number in every single object instance?

Comment
Add comment · Show 1
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 numberkruncher · Feb 29, 2012 at 01:34 AM 0
Share

Note: Error message is only logged when loading scene in editor, there are no problems when compiled.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by syclamoth · Feb 29, 2012 at 01:41 AM

Well, since you are using C#, I can give you plenty of better ways!

For starters, you shouldn't be using the constructor in a MonoBehaviour-derived class, for any reason. Any functionality you put in there should be invoked in the

 void Awake()
 {
 }

method, instead.

If you want to make a lazily-evaluated constant int for the layer, try doing it like this:

 int _projectileLayer = -1;

 int projectileLayer{
     get {
         if(_projectileLayer == -1)
         {
             _projectileLayer = LayerMask.NameToLayer("Projectile");
         }
         return _projectileLayer;
     }
 }

Then, you can use 'projectileLayer', and it will only ever calculate the value once per run.

You should avoid using static variables, just on principle- after all, it is possible that you'll want to use the same script to shoot projectiles from several different layers at one point, so storing it in a static variable will limit you in the indeterminite future.

In any case, you're right to try to speed things up- although keep in mind that in the timeless words of Donald Knuth: "premature optimisation is the root of all evil".

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 numberkruncher · Feb 29, 2012 at 01:48 AM 0
Share

I have been using Start to construct my objects, I will have to look into the difference between Awake and Start! I will take that approach, cheers!

avatar image neonblitzer · Oct 20, 2015 at 12:45 PM 0
Share

First, static constructors are okay as long as you don't use Unity functions. Second, usually you want to perform expensive operations at the start of a scene to reduce hiccups, i.e. avoid lazy loading. Third, the questioner was trying to avoid storing the data separately in each instance.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

A game object can only be in one layer. The layer needs to be in the range [0...31] 6 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