- Home /
Cannot cast from source type to destination type
Yes i searched this up without being able to solve me problem.
I have a pretty lengthy script to function the menu of my game. I am trying to disable all the text MeshRenderers at certain points. So i am using:
var meshRenderer : MeshRenderer[];
meshRenderer = GetComponents(MeshRenderer);
seems to be alright. however the error shows. This happens when i place the code in 2 scripts of my game. However i just tried on another one this error does not show up. I need this code in one of the two scripts. I dont think anything should be interfering with this code.
Any suggestions as to what might be causing this?
Answer by destructivArts · Jun 22, 2014 at 08:27 PM
You are trying to cast a MeshRenderer to an Array of MeshRenderers. Either make it
var meshRenderer : MeshRenderer;
Or:
var meshRenderer : MeshRenderer = new MeshRenderer[x];
meshRenderer[y] = GetComponent(MeshRenderer);
Note: I usually work with C# so if the syntax for creating an array in the second code snippet is wrong, I apologize. Just know that for the second one to work, you'd have to initialize the array and store your MeshRenderer at one of its indices
Hope this helps! Best, Peter
okay, i was able to do what i wanted with the following script:
for(var text : $$anonymous$$eshRenderer in GameObject.FindObjectsOfType($$anonymous$$eshRenderer)){
if(text.name == "New Text"){
text.GetComponent($$anonymous$$eshRenderer).enabled = false;
}
}