G
G
Gefist2022-02-19 18:38:30
Unity
Gefist, 2022-02-19 18:38:30

How to assign an element (eg GameObject from another script) to a prefab script?

Hello, this is a very dumb question, sorry.
I have a prefab with a script and I need to hang GameOdjeck on it, but it does not hang, but if the prefab is transferred to the scene, GameOdjeck is hung, and yes, I know that the script on the prefab in the hierarchy will not receive anything, that's why I'm asking.
Please tell me how to hang GameOdjeck on the prefab script.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gefist, 2022-02-19
@Gefist

The reason, I believe, is that the class hasn't been created yet... mono behavior classes are created (instantiated) in the editor for drag and drop purposes, etc. The editor and the runtime are two completely different areas, and doesn't make sense make the runtime class available in the editor if there is no reason to. Thus: extends MonoBehaviour
this should solve your problems:
1) The class must be created: var myClass=new MyClass();
2) The class must be the same: 'name' == 'script name': var myClass:MyClass=new MyClass();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question