Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
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

1 Answer 1

up vote 0 down vote accepted

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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