Search Unity

My Collision layer isn't working

Discussion in 'Editor & General Support' started by OneSketchyGuy, Apr 7, 2019.

  1. OneSketchyGuy

    OneSketchyGuy

    Joined:
    Oct 27, 2016
    Posts:
    14
    I'm trying to get this code to set the layer of a stored object after changing it earlier...
    Here is the stored Layer code.

    Code (CSharp):
    1.     public struct StoredRenderer
    2.     {
    3.         public GameObject gameObject { get; private set; }
    4.         public int layer { get; private set; }
    5.  
    6.         public StoredRenderer(GameObject gameObject, int layer)
    7.         {
    8.             this.gameObject = gameObject;
    9.             this.layer = layer;
    10.         }
    11.  
    12.         public void ResetGameObjectLayer()
    13.         {
    14.             gameObject.layer = layer;
    15.         }
    16.     }
    Here is where I use it

    Code (CSharp):
    1. public void UpdateCharacterLayerAlpha()
    2.         {
    3.             CharacterLayer.color = new Color(CharacterLayer.color.r, CharacterLayer.color.g, CharacterLayer.color.b, currentLayerAlpha);
    4.  
    5.             RaycastHit hit;
    6.  
    7.             Vector3 start = Camera.main.transform.position;
    8.  
    9.             Vector3 dir = Camera.main.transform.forward;
    10.  
    11.             float dist = Vector3.Distance(start, player.transform.position) - 0.5f;
    12.  
    13.             if (Physics.Raycast(start, dir, out hit, dist, layerMask))
    14.             {
    15.                 Debug.DrawRay(start, dir * hit.distance, Color.yellow);
    16.  
    17.                 if (hit.transform != null)
    18.                 {
    19.                     GameObject obj = hit.transform.gameObject;
    20.  
    21.                     int layer = obj.layer;
    22.  
    23.                     StoredRenderer stored = new StoredRenderer(obj, layer);
    24.  
    25.                     if (dontRenderers.Contains(stored) == false)
    26.                     {
    27.                         dontRenderers.Add(stored);
    28.  
    29.                         stored.gameObject.layer = LayerMask.NameToLayer("TransparentFX");
    30.                     }
    31.  
    32.                     if (currentLayerAlpha < maxLayerAlpha)
    33.                         currentLayerAlpha += alphaSpeed * Time.deltaTime;
    34.                 }
    35.                 else
    36.                 {
    37.                     if (currentLayerAlpha > 0)
    38.                         currentLayerAlpha -= alphaSpeed * Time.deltaTime;
    39.  
    40.                     ClearDontRenders();
    41.                 }
    42.             }
    43.             else
    44.             {
    45.                 Debug.DrawRay(start, dir * dist, Color.white);
    46.  
    47.                 if (currentLayerAlpha > 0)
    48.                     currentLayerAlpha -= alphaSpeed * Time.deltaTime;
    49.  
    50.                 ClearDontRenders();
    51.             }
    52.         }
    53.  
    54.         List<StoredRenderer> dontRenderers = new List<StoredRenderer>() { };
    55.  
    56.         int attempts = 0;
    57.  
    58.         void ClearDontRenders()
    59.         {
    60.             if (attempts >= 3)
    61.             {
    62.                 Debug.LogError("Unable to clear list");
    63.  
    64.                 attempts = 0;
    65.  
    66.                 Debug.Break();
    67.  
    68.                 return;
    69.             }
    70.  
    71.             foreach (var dr in dontRenderers.ToArray())
    72.             {
    73.                 dr.ResetGameObjectLayer();
    74.             }
    75.  
    76.             if (allObjectsVisable(dontRenderers.ToArray()))
    77.             {
    78.                 Debug.Log("Cleared dont renders");
    79.  
    80.                 dontRenderers.Clear();
    81.  
    82.                 attempts = 0;
    83.             }
    84.             else if (attempts < 3)
    85.             {
    86.                 attempts++;
    87.  
    88.                 ClearDontRenders();
    89.             }
    90.         }
    Attempts is just to prevent any stack over flow exceptions.

    Any suggestions?
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    Is ResetGameObjectLayer() getting called? I'd say just put some breakpoints (or some additional Debug.Log if you prefer) at the key points in your logic.

    A couple of general suggestions:
    • I'd generally check that gameObject isn't null before accessing it.
    • I don't see any reason to use ToArray() on your collection if you're not modifying the collection.
    • Setting a layer doesn't set the layer of any child objects. You might want to consider a recursive approach thasets child layers.
    • I don't think that `dontRenderers.Contains(stored)` could ever be true. You're checking whether your collection contains the struct you just created, which it never will. You can override Equals on your struct so that it considers itself equal to another StoredRenderer if they share the same gameObject. Or check whether the collection contains a storedRenderer whose gameObject is the one you're adding.
     
    OneSketchyGuy likes this.
  3. OneSketchyGuy

    OneSketchyGuy

    Joined:
    Oct 27, 2016
    Posts:
    14

    Solution!
    I wound up taking your last tip and created this function {

    private bool dontRenderersContainsObject(GameObject go)
    {
    foreach (var item in dontRenderers)
    {
    if (item.gameObject == go) return true;
    }

    return false;
    }
    }

    Thanks allot!