2
\$\begingroup\$

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

\$\endgroup\$
9
  • \$\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\$ Commented May 13, 2013 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
    Commented May 13, 2013 at 22:27
  • \$\begingroup\$ @Tetrad Using only GetComponent I get a null object \$\endgroup\$ Commented May 14, 2013 at 9:52
  • \$\begingroup\$ @BenjaminDangerJohnson I need to attach the scripts to those Monobehaviour components \$\endgroup\$ Commented May 14, 2013 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
    Commented May 14, 2013 at 10:23

1 Answer 1

0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ In effect you cannot change Monobehaviour scripts in runtime. I just edited the prefab to include the scripts and solved the problem. \$\endgroup\$ Commented May 15, 2013 at 15:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .