Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by wxxhrt, Jan 13, 2015.

  1. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    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
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    from classA
    b.setA(this);

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

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    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...
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Not sure. If you are instantiating, it saves you using get component.
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    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.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  7. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    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!
     
  8. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    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.
     
  9. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    I thought you had it working.
     
  10. wxxhrt

    wxxhrt

    Joined:
    Mar 18, 2014
    Posts:
    163
    So did I!:)

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