- Home /
instantiated gameobjects(clone) don't have components of its original gameobject.
Hey guys... I have 10 same objects in scene. I have a script that is attached to all those objects and some of the code of that script is as follows:
Destroy (gameObject);
Object.Instantiate (gameObject, position, Quaternion.identity);
after destroying those 10 gameobjects, I instantite those objects at same time as shown in code. but, the problem is that when I instantiate those gameobjects, they have disabled components of destroyed gameobjects like script, audio source etc. I got a solution that shows that if i first instantiate object and then destroy the original ones, then this problem is solved. But its not suitable solution for me because if i first instantiate and then destroy originals, then the new instantiated objects wont stay on floor, they fall, as I have tried it. So I want a solution of it that gives ENABLED COMPONENTS of new instantiated gameobjects. pls help if any one have a solution.
Why can't you just Enable/Disable a gameObject ins$$anonymous$$d of Instantiating/Destroying?
Answer by Cherno · Jun 26, 2015 at 11:46 AM
Instantiating first and then Destroying is the way to go. If the new gameObjects fall, that's a problem with their physics, not with the way they are instantiated. It would help to know if the objects have rigidbodies and colliders and how these are set up.
yes. objects have rigidibodies, so sometimes they fall and sometime stands on floor. i dont know what to do. now i destroy first and then instantiate as shown below:
Destroy (gameObject);
Object.Instantiate (gameObject, position, Quaternion.identity);
and when i instantiate those objects simultaneously as mentioned, the game in play mode chock ups for 1-2 seconds at the time of the instantiation.
and for getting the components, I use the following code:
GameObject GO = Instantiate (gameObject, position, Quaternion.identity) as GameObject;
GO.GetComponent<Script> ().enabled=true;
GO.GetComponent<AudioSource> ().enabled = true;
Ok, so you are making a copy of the very gameobject this script is attached too. I suspect that the rigidbodies of both objects collider upon instantiation. Try setting the original go's rigidbody to Is$$anonymous$$inematic = true, then instantiate the copy, then set the copy's rigidbody to Is$$anonymous$$inematic = false, and then destroy the original gameobject.
i cant keep original gameobject's Is$$anonymous$$inetic=true because if I keep that, then they wont collide and I want them to collide and behave as a physic dynamic component, not as static. BTW now script is running fine but the only problem is that when I instantiate objects, game/camera chokes up for 1-2 seconds. 10 objects instantiating simultaneously is the problem I think. But that is what I needed. Don't know how to stop being choke up the game !
Your answer
Follow this Question
Related Questions
Instantiated Object's Scripts not enabled? Not sure why 3 Answers
Adjusting Rotation of Instantiated Objects 0 Answers
Trying to Clone 1 of 5 GameObjects in a array 1 Answer
Instantiating a GameObject from scene that changes 1 Answer
How can I destroy an specific clone already instantiated, when there's more than one? 1 Answer