- Home /
Trouble spawning cloned objects
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;
}
}
}
Answer by sparkzbarca · Oct 12, 2012 at 07:08 PM
when you play and it "spawns" see if an object is added to the hierarchy off to the bottom left. if it is its spawning.
Now my guess is that the problem is IsKinematic doesnt actually stop it from messing with other objects movement and the object is in fact stuck inside the other. If thats true what you should be able to do is see that by.
1. seeing a new object generated.
2. pausing the game
3. seeing the position of the clone is identical to where it should be
4. moving the position of the base spawn object and unpausing and now seeing the object drop.
the solution if this is the case.
1.Create an empty game object at all 4 locations exactly at where the CENTER of the spawned object should be.
2. use Instantiate (original : Object, position : Vector3, rotation : Quaternion) so
Instaniate(SpawnObject,Empty_Game_Object_Spawn_Point.position, Empty_Game_Object_Spawn_Point.rotation);
hope that helps
Your answer
Follow this Question
Related Questions
How to get movable/pushable cubes right? 0 Answers
change gravity after time 1 Answer
My Objects all fall Unrealistically SLow with the deafult Gravity Even with Drag ZERO, what to do? 1 Answer
Respawning with no gravity 3 Answers
How to get drag to affect an aircraft velocity but not the gravity of the aircraft? 0 Answers