- Home /
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;
}
}
The issue with your code is that "goods " variable is assigned with the value only once that is at start method(Start method gets called only once in its life time as soon as you hit play button). Though the condition if(theCountdown
void SpawnGoodies()
{
// Defines the $$anonymous$$ and max ranges for x and y
Vector2 pos = new Vector2 (Random.Range (x$$anonymous$$in, x$$anonymous$$ax), Random.Range (y$$anonymous$$in, y$$anonymous$$ax));
// goods now represents the random object within the array
goods = theGoodies [Random.Range (0, theGoodies.Length)];
// Creates the random object at the random 2D position.
Instantiate (goods, pos) as GameObject;
}
hope this may help you. NS$$anonymous$$S
Answer by wibble82 · Dec 15, 2015 at 11:19 AM
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;
}
//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 x$$anonymous$$in;
public float x$$anonymous$$ax;
// the range of y
[Header ("Y Spawn Range")]
public float y$$anonymous$$in;
public float y$$anonymous$$ax;
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 $$anonymous$$ and max ranges for x and y
Vector2 pos = new Vector2 (Random.Range (x$$anonymous$$in, x$$anonymous$$ax), Random.Range (y$$anonymous$$in, y$$anonymous$$ax));
// 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, transform.rotation);
// If I wanted to get the result of instantiate and fiddle with it, I might do this ins$$anonymous$$d:
//GameObject newGoods = (GameObject)Instantiate(goodsPrefab, pos)
//newgoods.something = somethingelse;
}
}
The following code is a complete functional result of the wonderful help I received from you all. The only thing that needed to be "added" was:
Instantiate (goodsPrefab, pos, transform.rotation);
It threw an error asking for 2 arguments.. so i put the "transform.rotation" in there and it works perfectly.. Thank you all!
Answer by FAL0 · Dec 15, 2015 at 02:48 AM
// Creates the random object at the random 2D position. Instantiate (goods, pos) as GameObject;
as GameObject
@FAL0, I approved of your post, but it's not a very good answer. Please edit it with a solution and more details to avoid down votes.
Just delete the as GameObject.
It is only used if the object is assigned.
Your answer
Follow this Question
Related Questions
instantiate a random object from multiples via 'tag' 1 Answer
multiple object in multiple spawn Point with out repeat 0 Answers
Spawning Objects Using An Array. 1 Answer
How to instantiate the first 8 gameobjects in an array (UnityScript)? 1 Answer
Randomly instantiate objects from array without choosing the same item twice. 2 Answers