I am attempting to have objects spawn in one of four locations horizontally and then have gravity take effect and allow them to fall towards baskets located in corresponding locations on the ground.
I have placed the four original objects at the top of the screen in the four spawn locations and am attempting to randomise which of the four spawn locations is used, every four seconds. These original objects are being used as spawn points so that the duplicate objects will appear in the same location and fall from there. They have been set to kinematic (to allow the clones to appear through them), gravity is false (to avoid them falling) and their sphere colliders are disabled. I am attempting to change these settings in the code once the clone objects have been instantiated.
However, having attempted to code this in C#, I am finding that the objects never seem to spawn in. Can anyone offer some advice on this? Please note that the item prefabs are stored in the resources folder. Here is the code in the update method:
// Update is called once per frame
void Update () {
GameObject ItemClone1;
GameObject ItemClone2;
GameObject ItemClone3;
GameObject ItemClone4;
//Create a timer, starting on 0
double timer = 0.0;
//Increase the timer in real time
timer += Time.deltaTime;
//Sets the condition if the number of seconds divided by 4 leaves a remainder of 0
//In other words every 4 seconds
if(timer % 4 == 0)
{
//Generate a random number between 1 and 4
int spawnLocation = Random.Range(1,4);
//Determine the spawn position based on the random number
if(spawnLocation == 1)
{
ItemClone1 = (GameObject)Instantiate(Resources.Load("ItemPrefab1"));
ItemClone1.AddComponent("Rigidbody");
ItemClone1.rigidbody.useGravity = true;
ItemClone1.rigidbody.drag = 10.0F;
ItemClone1.rigidbody.isKinematic = true;
}
if(spawnLocation == 2)
{
ItemClone2 = (GameObject)Instantiate(Resources.Load("ItemPrefab2"));
ItemClone2.AddComponent("Rigidbody");
ItemClone2.rigidbody.useGravity = true;
ItemClone2.rigidbody.drag = 10.0F;
ItemClone2.rigidbody.isKinematic = true;
}
if(spawnLocation == 3)
{
ItemClone3 = (GameObject)Instantiate(Resources.Load("ItemPrefab3"));
ItemClone3.AddComponent("Rigidbody");
ItemClone3.rigidbody.useGravity = true;
ItemClone3.rigidbody.drag = 10.0F;
ItemClone3.rigidbody.isKinematic = true;
}
if(spawnLocation == 4)
{
ItemClone4 = (GameObject)Instantiate(Resources.Load("ItemPrefab4"));
ItemClone4.AddComponent("Rigidbody");
ItemClone4.rigidbody.useGravity = true;
ItemClone4.rigidbody.drag = 10.0F;
ItemClone4.rigidbody.isKinematic = true;
}
}
}