Two Scripts on same object question

I have two scripts on the same object. The first script Piece.cs just holds a some data about the object in somepublic variables, one of them variables is an interger called “Faction”. The other script PC.cs responds to collisions. When a trigger collider goes off the script PC.cs executes some code. In that code I need to grab some data that exists in Piece.cs so I was hoping I could just reference that script from inside PC.cs something like this…

int faction = Piece.Faction;

kinda like he way you can reference the transform of he object, but I can’t I have to do this…

private Piece thisPiece = null;			//the Piece script on this object
.
.
.
thisPiece = gameObject.GetComponent<Piece>();
int faction = thisPiece.Faction;

I’m cool with doing it this way, but if there is a way to directly get the Faction property in the Piece.cs class, I rather do it that way.

That’s pretty much the way to do it. However, for elegance and speed and less typing, what you can do is cache that component reference in the Awake method for the script so that you don’t have to keep looking it up.

A property can also be used to retrieve the component and cache it if it has not already been set, which may be architecturally cleaner and lead to a reduced likelihood of accidentally re-setting the reference.

Unfortunately, you cannot declare your component reference member variable readonly as the Awake method is not considered a constructor. so you lose that language facility. :frowning:

Thanks again Justin.