- Home /
Reference to Scene Object Component in Prefab Editor, 'Type Mismatch'?
I have a reference to a component on an object in my scene, stored in a component in my prefab. Declared like this:
public Component component;
I have a custom editor that uses this line to display it:
EditorGUI.PropertyField( tempPosition, property.FindPropertyRelative("component"));
The Inspector shows "Type Mismatch" where the component should be.
What's strange is my reference works fine when I apply a component from the same object, or from a component on an object in the scene. Also, when I instantiate the prefab, the instance shows the reference as expected, that is: 'object (component)'.
So really everything is working fine except my custom editor looks a little stupid when it says 'Type Mismatch' on my prefab. Anyone have any clues what this might be?
then why have a Component
reference if you just want $$anonymous$$onoBehaviours
? Try using a $$anonymous$$onoBehaviour
ins$$anonymous$$d of Component
. do you still get that miss-match?
I actually want to allow Components as well, though I'm mostly working with mono-behaviors right now.
Point is, it works perfectly in almost every way. The data is there and functioning... it's seems to be just the editor display that is not working properly... so I don't imagine there's anything very wrong with how I'm doing it... maybe just something that needs to be cast or somesuch. I can't even really test to see if it's happening because when I do a print of what's stored in the field, it reports that I have my behavior (or component) and everything looks normal. No error messages in debug or anything.
Actually, turns out everything only works perfectly until you reload the scene. Then the problem settings don't work. Are prefabs not allowed to hold references to instantiated objects, maybe?
Answer by sun_spider · Mar 01, 2014 at 09:34 PM
I found some info on this. It has to do with prefabs not being able to contain references to GameObjects in the scene. I don't see why Unity couldn't make this work, but I created a workaround involving using inactive GameObjects instead of prefabs, and instantiating them instead. Works fine, but would be nice if prefabs didn't have this constraint.
It doesn't work because 1) prefabs are just designs/blueprints for an object, not a 'real' object, and 2) they can be reused in multiple scenes. You can link internally. The typical workaround is to have the prefab find and initialize links in Start() using things like GameObject.Find() or GameObject.FindWithTag().
I get that and it makes sense. In thinking about it I guess there should just be a more obvious warning when you try to do this.
Your answer
Follow this Question
Related Questions
Editor: Is it possible to move a component of a Prefab Gameobject Child to it's parent?? 1 Answer
Inspector:Reference a component in Assets, not in scene. 0 Answers
How to keep reference linked when replace prefab through editor scripts? 1 Answer
Instantiating scripts with references to prefabs loses the reference. 1 Answer
Targeting parts of a Prefab more than 1 level down? 1 Answer