- Home /
My code works but produces an error in the console, not sure what's wrong but I would like to fix it.
Following the guide in the use of GetComponentsInChildren, and using the code below to have a toggle button that turns on and off trails on object(TrailRenderer). I get the ErrorInvalidCastException: Cannot cast from source type to destination type. I've looked up the error but can't make much sense of it from the examples I've found. I am happy the code works, the trails turn on and off easily! But I think the error means I'm doing something wrong. Thank you!
var trailrend : TrailRenderer[];
trailrend = gameObject.GetComponentsInChildren(TrailRenderer);
var curt = false;
function OnMouseDown() {
if (!curt) {
for (var rend : TrailRenderer in trailrend) {
rend.enabled = true;
}
} else {
for (var rend : TrailRenderer in trailrend) {
rend.enabled = false;
}
}
curt = !curt;
}
Answer by CharlesD · Aug 18, 2012 at 07:52 AM
Ah well this isn't a biggy. What's happening is that GetComponentsInChildren returns an object of type "Component[]" despite what specific type of component you wanted it to find. So when Javascript attempts to store a variable of type "Component[]" into a variable specified as type "TrailRenderer[]" it realizes that the data types do not match and cannot be converted to one another automatically. So long story short all you have to do is change this...
trailrend = gameObject.GetComponentsInChildren(TrailRenderer);
to this...
trailrend = gameObject.GetComponentsInChildren(TrailRenderer) as TrailRenderer[];
The extra statement tells Javascript that you want to format GetComponentsInChildren's return value as an array of TrailRenderers rather than an array of Components. I have not tested this as I am on my phone, but given the error and my knowledge of the C# version of the method, I believe this should fix it.
Hey! Thanks for getting back to me. I added the changes you suggest and while it did make the console error disappear it actually disabled my button/Trails in a very odd way. So this is what happens: With your code above I can place my Trails into the variable array in the inspector, just as before, but then when starting the game it removes them immediately from that array, it basically clears it. On stopping the gane they are there again, so it's just when it's run. I have one or two ideas what it might be(the #Start or #Awake functions?) but other than that I don't know. Anyway, thanks for your help, I did of course back up my original code so no worries.
Hmmm... The code you showed above in your question actually does not need you to explicitly add the trail renderers to the array, it automatically searches the gameObject it is attached to and its children for all TrailRenderers and adds them to the array, rewriting whatever it is you assigned to it to begin with. What I assume to be happening is that there are no children of the gameObject you attached this script to (or the children do not have TrailTenderers) and therefore the method is returning an empty array. If you want to manually set which TrailTenderers are in the array then completely comment out the line I asked you to change. If you do want the script to set the array automatically then the gameObject you dragged the script onto or any one of its children need to have a TrailRenderer component attached. However I would personally recommend the first solution as it sounds more like what you wanted to begin with. Good Luck.
YES! That totally worked! Thanks so much for sticking through it with me Charles!