Hi,
I recently just finished the lecture Spawn At Mouse Position.
In the video, Rick uses an “inception” kind of way that like the code below.
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;
}
}
I solved the problem by just plugging the GetSquaredClicked method like this
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;
}
}
Could you explain the advantage of using RIck’s way than the simple and easy just plugging the method into SpawnDefender?
Is there any long-term disadvantage using the second way?