get the instance of class defined in scripting file

I have a scripting file “PieceLogic.cs” added as a component of a GameObject, Piece. In the scripting file I have a class “PieceLogic”.

Now in another piece of script attached to a different GameObject, I have a reference of Piece. How do I get the instance of PieceLogic associated with Piece?

I tried these, which do not work:

PieceLogic pl = Piece.GetComponent<PieceLogic>("PieceLogic.cs");

and

PieceLogic pl = (PieceLogic)Piece.GetComponent("PieceLogic.cs");

2 Answers

2

If I understand your question correctly, you have a GameObject with PieceLogic and Piece? And you would like to get PieceLogic when you have Piece? You can do you this

PieceLogic pieceLogic = Piece.gameObject.GetComponent <PieceLogic>();

One way is to do something like
PieceLogic pl = GameObject.Find("Piece").GetComponent() in order to access another object’s script. See the articles at UnityGems for a detailed explanation of this and other methods.

That should be PieceLogic pl = GameObject.Find("Piece").GetComponent<PieceLogic>() Don't know why I'm having problems editing that.