S
S
Spartanec332021-07-09 19:42:20
Unity
Spartanec33, 2021-07-09 19:42:20

How to efficiently transfer data from one script to another?

I have 2 scripts. No. 1 hangs on some empty object and it contains only data for some functions.
And there is script number 2, which contains the same functions. There is also, well, let's say 1000 different prefabs, and script #2 hangs on each. How to efficiently get data from script #1? I need to leave the ability to edit through the inspector of one object on which script No. 1, so that everything on which script No. 2 changes, to avoid code duplication in order to be able to add new data without problems and fill in a bunch of fields manually. I understand that you can use FindObjectOfType in script #2, but this is a performance hit and still duplicating code, you have to take values ​​from script #1 and insert them into the fields of the second one. You can create a static intermediary class that uses FindObjectOfType 1 time and then take values ​​from it into the second script, so the performance problem is solved, but now there is also a class which has the same fields as classes No. 1 and No. 2, that is, if I want one more data field, then edit all 3 classes. I also thought about inheritance, it’s trivial to inherit script #2 from script #1, then they have the same fields and you don’t need to rewrite anything, but how to transfer field values? People, maybe I completely went to the side and for this there is a simple solution that I don’t see?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Griboks, 2021-07-10
@Spartanec33

ScriptableObject

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question