Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by losingisfun · Oct 09, 2016 at 05:24 AM · c#instantiate prefabprojectiles

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);
 }


Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JedBeryll · Oct 09, 2016 at 05:34 AM 0
Share

Are you sure you are changing the instance, not the original object?

avatar image Bonfire-Boy · Oct 09, 2016 at 07:33 AM 0
Share

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.

avatar image losingisfun Bonfire-Boy · Oct 09, 2016 at 07:53 AM 0
Share

I'm definitely cloning them, how would I avoid cloning? (Also, is would it be significantly clunkier?) - ill show my code

avatar image Bonfire-Boy losingisfun · Oct 09, 2016 at 07:57 AM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Oct 09, 2016 at 09:18 AM 0
Share

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.

avatar image losingisfun Bonfire-Boy · Oct 09, 2016 at 09:34 AM 0
Share

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?

avatar image Bonfire-Boy losingisfun · Oct 09, 2016 at 09:43 AM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

227 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges