Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

A problem with downcasts (Did not occur until unity5)

Discussion in 'Unity 5 Pre-order Beta' started by Warrior1424, Feb 16, 2015.

  1. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    After upgrading a project, I got one warning that I can not seem to fix.

    At this line:
    Code (JavaScript):
    1. for(var bits : MeshRenderer in mrenders)
    2.         bits.enabled = false;
    Here's where the variable "mrenders" is established (a few lines before the code above):
    Code (JavaScript):
    1. var mrenders = gameObject.GetComponentsInChildren(MeshRenderer);
    Was something in the API changed that could cause this, or did I just not notice this warning before?
    I kind of get what a downcast is, I just don't how to fix it in this instance.
     
  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,493
    Instead of doing

    Code (csharp):
    1.  
    2. gameObject.GetComponentsInChildren(MeshRenderer);
    3.  
    try

    Code (csharp):
    1.  
    2. gameObject.GetComponentsInChildren.<MeshRenderer>();
    3.  
     
    AdrianoVerona_Unity likes this.
  3. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
unityunity