Sometimes, I find the need to reference a game object that contains a script with a Class1
In the script that references this object should I write
public GameObject foo
And then get the Class1 by
foo.GetComponent()
or
should I write
public Class1 foo
And get the game object by
foo.gameObject
Which one is best practice?
Both. Keep a reference to the class, and the gameobject, and don’t use get component more than once, that way their both readily available to access through code.
Though if I remember correctly, the something.gameObject member is a cached reference, so it’d be probably the exact same performance. I could be wrong about that though.
Anyway I keep refs to both, just for easier use later, less typing haha.