0
\$\begingroup\$

I have a class inherited to another class, and I can't get the transform on the inherited class.

Here's what i have and trying to do

Class A:Monobehavior{}

Class B:A{}

//somewhere else

A object = new B();

but right now, whenever I try to get the transform of object, either from a function in B or out here via object.transform, it will always return null.

How do I fix this?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Because what you have is a lone Monobehavior in a void.

It's not on any GameObject and it's not in the Transform tree.

You need to create it right on a GameObject by using AddComponent:

A object = some_game_object.AddComponent<B>() as A;
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.