# Get MonoBehaviour components from Prefab

I need to assign the script values for the following Prefab in Unity 3.5

The structure is the following


Kinect_Prefab

 -MonoBehaviour

·Script: Missing Mono Script

-MonoBehaviour

·Script: Missing Mono Script

-MonoBehaviour

·Script: Missing Mono Script

-MonoBehaviour

·Script: Missing Mono Script


All the components have the same name.

I have tried to obtain all of them in an array, but the GetComponents method does not seem to work properly.

var mono : MonoBehaviour[];
mono = Kinect_Prefab.GetComponents(MonoBehaviour);


The thing is the resulting array is empty.

Any idea what might be wrong?

EDIT: When I do GetComponents(MonoBehaviour) I get the following error

 InvalidCastException: Cannot cast from source type to destination type. 

I have tried receiving the components in var mono : Component [] but I still don't receive anything

• Normally people would get the components like this "Kinect_Prefab.GetComponents<MonoBehaviour>();" but unless you are receiving a compilation error I doubt that is your problem. Could the scripts you are looking for be attached to children of the prefab? – Benjamin Danger Johnson May 13 '13 at 21:19
• If you try it with GetComponent do you get a single component back? Why are you using the base class MonoBehaviour instead of something you implemented? – Tetrad May 13 '13 at 22:27
• @Tetrad Using only GetComponent I get a null object – Anton Garcia Dosil May 14 '13 at 9:52
• @BenjaminDangerJohnson I need to attach the scripts to those Monobehaviour components – Anton Garcia Dosil May 14 '13 at 9:54
• When you say "attach the scripts" do you mean you need to assign a variable to a script you made or do you mean you need to attach the script you made to the prefab itself? For the former you need to get the specific derived type so you can access the variable. For the latter you want to AddComponent instead and what you're asking doesn't make any sense. What does the prefab look like in the inspector? – Tetrad May 14 '13 at 10:23

I might be completely off... but it seems like you have no scripts attached to the Kinect_Prefab - that's why it says Script: Missing Mono Script. This means that scripts were previously attached but they were deleted (did you rename them?)
Once you've done that, your other scripts will be able to access the MonoBehavior objects of the attached scripts.