Whats the difference between GetComponent and (Player)FindObjectOfType() ?
I am new to c# and game programming in general and while trying to learn about gizmos, i ran into a problem. My code for gizmos is as follows: void OnDrawGizmos() {
GameObject go;
Player pl = GetComponent<Player>();
go = pl.gameObject;
Gizmos.DrawWireCube(new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z+2),
new Vector3(1, 1, 1));
Gizmos.DrawLine(gameObject.transform.position, go.transform.position);
}
Now this is raising the following exception:
NullReferenceException: Object reference not set to an instance of an object MyGizmos.OnDrawGizmos () (at Assets/practise scripts/Gizmos/MyGizmos.cs:18) UnityEditor.DockArea:OnGUI()
i dont know where i'm going wrong...any help would be immensely appreciated. thank you
Difference is FindObjectOfType searches the entire scene and GetComponent searches the specific gameobject. This does not say which line has the error but most likely the gameobject that this is attached to does not have a Player component (if it did pl.gameobject would not make sense because it's the same gameobject)