This script runs on an empty gameobject, and it’s iterating through its children, like so:
foreach (NavPoint current in gameObject.transform) {
}
“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.
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.
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.
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.