Search Unity

This has got to be simple to fix - casting problem inside a foreach loop?

Discussion in 'Editor & General Support' started by Dubious-Drewski, Jan 6, 2015.

  1. Dubious-Drewski

    Dubious-Drewski

    Joined:
    Aug 19, 2012
    Posts:
    51
    This script runs on an empty gameobject, and it's iterating through its children, like so:

    Code (CSharp):
    1. foreach (NavPoint current in gameObject.transform) {
    2. }
    "NavPoint" is simply a custom class which (so far) doesn't do anything except hold some variables (including a Transform variable called "actualPoint") When I run this, I get a "Cannot cast from source type to destination type".

    I understand why I'm getting the error, but I cannot make it work. How do I make these types match? All my Google results are for similar, but different problems than this.
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    It is impossible to cast a Transform to a NavPoint, you need to access the NavPoint from the Transform.
    Code (csharp):
    1. foreach (Transform current in gameObject.transform) {
    2.     NavPoint navPoint = current.GetComponent<NavPoint> ();
    3.     // Do what you want
    4. }
     
  3. Dubious-Drewski

    Dubious-Drewski

    Joined:
    Aug 19, 2012
    Posts:
    51
    Thank you for the reply!

    This doesn't do it though. I should have specified that the child objects (navPoints) do not have individual scripts on them, so using GetComponent on them doesn't work. The parent object is simply running the script and adding each navPoint to a List.
     
  4. Dubious-Drewski

    Dubious-Drewski

    Joined:
    Aug 19, 2012
    Posts:
    51
    Eureka!

    I found a way around the problem. Just in case anyone else wonders: First I make a list of my navigation points:
    Code (CSharp):
    1. foreach (Transform thisPoint in gameObject.transform)
    2. {
    3.       navPoints.Add(new NavPoint(10, thisPoint));
    4. }
    Then I iterate through the List, not through the Transform's child objects, like I was doing.
    Code (CSharp):
    1. foreach (NavPoint current in navPoints)
    2. {
    3. }
    Thanks for the help anyway!
     
  5. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Code (CSharp):
    1. gameObject.transform.Cast<Tranform>().Select(child => new NavPoint(10, child))
     
  6. Dubious-Drewski

    Dubious-Drewski

    Joined:
    Aug 19, 2012
    Posts:
    51
    Jessy: Well then! That's exactly what I was after, although I do not fully understand what is happening in that line or how it works.

    I'm going to copy it down and study it. But for now I'll just use my less-elegant solution because I understand it and can debug it if something goes wrong.

    Thank you! I can't wait until the day when things like these don't stump me anymore.
     
  7. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  8. BMayne

    BMayne

    Joined:
    Aug 4, 2014
    Posts:
    186
    and Linq!

    Just keep in mind when using Linq. If you use that code and NavPoint is a class everything should be fine. If it's a struct and are building for ios you are going to have a bad time.