- Home /
C# - stop instantiated objects from inheriting the last
I have a problem, each time i instantiate a projectile, it inherits attributes from the last one. For example, after i've instantiated an object, when it hits the ground, it disappears, by SetActive (false); and when i instantiate the next projectile I need to SetActive (true); because it gets instantiated as inactive.
Is there a trick to this? It's giving me a lot more problems than just that, for example it's colliders and rotation all need to be reset each time it's instantiated.
EDIT: providing some code
if (shootingProjectile) {
projectile = Instantiate (projectileToUse, transform.position, Quaternion.identity) as GameObject;
projectile.SetActive (true);
}
Are you sure you are changing the instance, not the original object?
This sounds normal, if you're cloning from the previous instance. Ins$$anonymous$$d, try instantiating from a prefab each time. And never modify the prefab itself.
I'm definitely cloning them, how would I avoid cloning? (Also, is would it be significantly clunkier?) - ill show my code
Yes it's still cloning. But you don't need to clone the one that just hit the ground. Clone the prefab every time, and leave the prefab itself alone. That way, every time you instantiate, the new instance will have the same properties as the prefab.
Answer by losingisfun · Oct 09, 2016 at 08:24 AM
By creating a placeholder GameObject I was able to prevent the cloned objects from inheriting each others values. Here is what I did instead:
if (shooting) {
projectile = projectileToUse;
projectile = Instantiate (projectile, transform.position, Quaternion.identity) as GameObject;
}
that way i was resetting the projectile prefab each time it was firing, instead of repeatedly cloning and getting variables mixed together.
Not sure if I'm understanding right, but you don't need to create a placeholder GameObject in the scene.
You can just create a prefab in the project view and hook it up to a public field in the inspector (in the component that creates the projectiles). Use that field as the first argument when you call Instantiate.
No i'm not creating another prefab or game object in the scene, i'm only assigning two GameObject variables and giving them the same prefab, and then instantiating projectiles from one of them, and resetting the first off the second, so it automatically resets it to the default prefab, so ins$$anonymous$$d of 5 lines of code, saying , "be active, rotate this way, turn collider on, change this boolean. " ins$$anonymous$$d i just say, "go back to what you started as" - get it?
Still not quite sure I understand, but the description makes it sound slightly over-complicated. If it works for you that's fine, of course, and it may just be that you're using the word "prefab" in a non-standard way.
Your answer
![](https://koobas.hobune.stream/wayback/20220612094300im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making a bubble level (not a game but work tool) 1 Answer
Bullet ricochet 0 Answers
Not sure how to do this- destroy existing data in game obejct to prevent duplicates 1 Answer