Search Unity

Using an unsigned int as a Culling Mask?

Discussion in 'Scripting' started by bdsantos, Jul 25, 2019.

  1. bdsantos

    bdsantos

    Joined:
    Jul 27, 2016
    Posts:
    109
    Hey, guys. So, I need a custom Culling Mask for my Main Camera. So, I'm working on a script to replace the default Culling Mask with a custom unsigned int. However, it keeps giving me conversion errors. Does anybody know how I can use an unsigned int?

    Here's my script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ChangeCullingMask : BaseScript
    5. {
    6.     private uint camMask = 4261412347;
    7.  
    8.     void Start()
    9.     {
    10.         base.cam.camera.cullingMask = camMask;
    11.     }
    12.  
    13. }
    The BaseScript class used in this script is used so I can access my cameras with ease. The variable 'cam' refers to the Main Camera.

    If someone could please help me out with making my code work properly, it would be much obliged. Please and thank you for your time.
     
  2. Lysander

    Lysander

    Joined:
    Feb 24, 2013
    Posts:
    1,653
    This seems a bit pointless unless you're using an enumeration to represent it... well, even then really. What happens when the value is out of the range of what the property can accept?

    To answer the question though, you can do this several ways:
    Code (csharp):
    1. base.cam.camera.cullingMask = System.Convert.ToInt32(camMask); // this will throw an overflow exception if the value is too high
    2. base.cam.camera.cullingMask = checked((int)camMask); // same as above
    3. - or -
    4. base.cam.camera.cullingMask = (int)camMask; // this will overflow and have a negative value if it's too high
    5. base.cam.camera.cullingMask = unchecked((int)camMask); // same as above
     
  3. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    706
    Hmm. What error are you getting exactly?