Accessing a Class's... Owner? Instantiator?

Im trying to use Classes more but am still getting my head around them.

If Class A creates an instance of ClassB with

b = new ClassB();

is there a way for b it to access variables in Class A, using Unityscript?

Thanks

from classA
b.setA(this);

//in b
public void setA(classA a){
abc = a.abc;
}

Thanks, that worked a treat. Is this good practice or frowned upon, seems as though it wasn’t obvious so maybe I’m not laying my classes out nicely…

Not sure. If you are instantiating, it saves you using get component.

A class usually doesn’t need to know who instantiated it.

Note I say usually. Sometimes it does, because of how it operates.

There may be a situation where a class does stuff to another object that already exists, so it needs a reference to that object. But of course, that object doesn’t necessarily have to be the one who instantiated it… but it can be.

There are a few times I can think of where it needs to be the object that instantiated. This is usually because the 2 classes are directly coupled… their relationship is dependent on one another. An example of this could be say a object that can have children, so there’s a collection that represents the references to children.

class Container
class ContainerChildrenCollection

These two are tightly coupled, and ContainerChildrenCollection may have to communicate to Container when it’s contents change, and Container definitely needs to communicate with the collection.

Typically if you need a reference you should pass it in in the constructor. Something like

b = new ClassB(this);

Sometimes this is justified. More often it indicates poor structure. Think through very carefully if there is not a way you can make the class function without a reference to its creator.

Thanks for the quick replies

The two classes are tightly coupled, ClassB (Spawner) is a spawner responsible for instantiating and destroying prefabs which will be saved to an array in ClassA (Layer), ClassA needs access to the array to run functions on all of the prefabs at once, like spinning and scaling them, changing their materials.

There are multiple layers but each only has one spawner.

I laid them out this way so I didn’t have one huge class responsible for instantiating destroying and preforming functions on all the objects but maybe it’s should roll them all in to one…

I think I’ll sleep on it!

Following on from this…

As ClassB has a reference to ClassA in it’s constructor, which in turn has a reference to ClassB, in the inspector I see ClassA with a nested(triangle) for ClassB, clicking this reveals ClassB but has a reference to ClassA, this goes on and on forever.

Ive changed ClassB’s reference to ClassA into a private variable which stops this appearing in the inspector, is this enough or am I missing something… I can post stripped out classes if the above makes no sense.

I thought you had it working.

So did I!:slight_smile:

And I think it’s now working ok by setting ClassB’s reference to a private variable, but wanted to check…