So I solved the challenge by plugging my GetSquareClicked() method directly into the instantiation within SpawnDefender(). This is my code below
public class DefenderSpawner : MonoBehaviour
{
[SerializeField] GameObject defender;
private void OnMouseDown()
{
SpawnDefender();
}
private Vector2 GetSquareClicked()
{
Vector2 clickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2 worldPos = Camera.main.ScreenToWorldPoint(clickPos);
return worldPos;
}
private void SpawnDefender()
{
GameObject newDefender = Instantiate(defender, GetSquareClicked(), Quaternion.identity) as GameObject;
}
}
but instead in the course, we passed an argument through our SpawnDefender() method and then put GetSquareClicked() within SpawnDefender() when we call it in OnMouseDown()
public class DefenderSpawner : MonoBehaviour
{
[SerializeField] GameObject defender;
private void OnMouseDown()
{
SpawnDefender(GetSquareClicked());
}
private Vector2 GetSquareClicked()
{
Vector2 clickPos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
Vector2 worldPos = Camera.main.ScreenToWorldPoint(clickPos);
return worldPos;
}
private void SpawnDefender(Vector2 worldPos)
{
GameObject newDefender = Instantiate(defender, worldPos, Quaternion.identity) as GameObject;
}
}
Both approaches solve the problem of getting the defenders to spawn on the mouse position. What is the advantage to creating an argument within SpawnDefender() as oppose to my technique of just plugging GetSquareClicked() directly into the Instantiation? Is one approach more efficient in terms of code? Perhaps more clear to other coders in a collaborative project? Or is creating an argument better for scalability of larger projects?