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

The structure is the following



    ·Script: Missing Mono Script


    ·Script: Missing Mono Script


    ·Script: Missing Mono Script 


    ·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

  • \$\begingroup\$ 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? \$\endgroup\$ – Benjamin Danger Johnson May 13 '13 at 21:19
  • \$\begingroup\$ 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? \$\endgroup\$ – Tetrad May 13 '13 at 22:27
  • \$\begingroup\$ @Tetrad Using only GetComponent I get a null object \$\endgroup\$ – Anton Garcia Dosil May 14 '13 at 9:52
  • \$\begingroup\$ @BenjaminDangerJohnson I need to attach the scripts to those Monobehaviour components \$\endgroup\$ – Anton Garcia Dosil May 14 '13 at 9:54
  • \$\begingroup\$ 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? \$\endgroup\$ – 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?)

What you have to do, is remove all the existing script components that say that a script is missing, and attach new ones (by dragging scripts on to the prefab object):

Missing script

Removing component

Once you've done that, your other scripts will be able to access the MonoBehavior objects of the attached scripts.

| improve this answer | |
  • \$\begingroup\$ In effect you cannot change Monobehaviour scripts in runtime. I just edited the prefab to include the scripts and solved the problem. \$\endgroup\$ – Anton Garcia Dosil May 15 '13 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.