A
A
Alexander Pianov2021-08-16 16:59:28
Unity
Alexander Pianov, 2021-08-16 16:59:28

What is the best way to get to a component that is common to several classes?

ItemInfo is used in several classes and carries general information about objects.
Can this code be made more concise?

public void SetItemInfo(ItemInfo info)
        {
            if (GetComponent<InstanceWeapon>())
            {
                GetComponent<InstanceWeapon>().itemInfo = info;
            }

            if (GetComponent<InstanceLook>())
            {
                GetComponent<InstanceLook>().itemInfo = info;
            }

            if (GetComponent<InstanceSupport>())
            {
                GetComponent<InstanceSupport>().itemInfo = info;
            }
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2021-08-16
@GavriKos

Through the interface. GetComponent works great with it.
You can also try using the base class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question