C# Spawn Random Object, at Random 2D Location

Hello all! (struggling beginner coder here).

I want to Spawn a random object
within an array, at a random spot in
Vector2D space

Im still not quite understanding what im doing wrong. Thank you kindly in advance!


//Array of objects to spawn
	public GameObject[] theGoodies;
	GameObject goods;

	//Time it takes to spawn theGoodies
	[Space(3)]
	public float waitingForNextSpawn = 10;
	public float theCountdown = 10;

	// the range of X
	[Header ("X Spawn Range")]
	public float xMin;
	public float xMax;

	// the range of y
	[Header ("Y Spawn Range")]
	public float yMin;
	public float yMax;


	void Start()
	{
		// goods now represents the random object within the array
		goods = theGoodies [Random.Range (0, theGoodies.Length)];
	}



	public void Update()
	{
		// timer to spawn the next goodie Object
		theCountdown -= Time.deltaTime;
		if(theCountdown <= 0)
		{
			SpawnGoodies ();
			theCountdown = waitingForNextSpawn;
		}
	}


	void SpawnGoodies()
	{
		// Defines the min and max ranges for x and y
		Vector2 pos = new Vector2 (Random.Range (xMin, xMax), Random.Range (yMin, yMax));

		// Creates the random object at the random 2D position.
		Instantiate (goods, pos) as GameObject;
	}
	
}

Your code appears largely correct, aside from 2 little issues:

  • You don’t need the ‘as GameObject’ on the end of Instantiate. This is a ‘dynamic cast’. If you wanted to store the result of instantiate you would need to cast it to a game object (though as you know for a fact it is a game object you would use the faster ‘static cast’)
  • You are only randomly choosing the ‘goods’ to spawn once.

Here’s a tweaked version of that code:

   //Array of objects to spawn (note I've removed the private goods variable)
     public GameObject[] theGoodies;
 
     //Time it takes to spawn theGoodies
     [Space(3)]
     public float waitingForNextSpawn = 10;
     public float theCountdown = 10;
 
     // the range of X
     [Header ("X Spawn Range")]
     public float xMin;
     public float xMax;
 
     // the range of y
     [Header ("Y Spawn Range")]
     public float yMin;
     public float yMax;
 
 
     void Start()
     {
     }
 
     public void Update()
     {
         // timer to spawn the next goodie Object
         theCountdown -= Time.deltaTime;
         if(theCountdown <= 0)
         {
             SpawnGoodies ();
             theCountdown = waitingForNextSpawn;
         }
     }
 
 
     void SpawnGoodies()
     {
         // Defines the min and max ranges for x and y
         Vector2 pos = new Vector2 (Random.Range (xMin, xMax), Random.Range (yMin, yMax));
 
         // Choose a new goods to spawn from the array (note I specifically call it a 'prefab' to avoid confusing myself!)
         GameObject goodsPrefab = theGoodies [Random.Range (0, theGoodies.Length)];

         // Creates the random object at the random 2D position.
         Instantiate (goodsPrefab, pos);

		 // If I wanted to get the result of instantiate and fiddle with it, I might do this instead:
		 //GameObject newGoods = (GameObject)Instantiate(goodsPrefab, pos)
		 //newgoods.something = somethingelse;
     }
   // Creates the random object at the random 2D position.
     Instantiate (goods, pos) as GameObject;
        as GameObject