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 csgeorge · Nov 29, 2015 at 04:19 AM · c#instantiateinstantiation

Inconsistencies in Instantiation Code

I'm using the code from Unity's Instantiate tutorial (very slightly modified) to create projectiles in my project. However, I'm noticing that it works inconsistently. Most of the time when I click "Fire1" it creates one projectile properly. But other times it will either not create any projectile or create two of them that will launch at once. Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class UsingInstantiate : MonoBehaviour
 {
     public Rigidbody rocketPrefab;
     public Transform barrelEnd;
     public int shotSpeed;
     
     
     void FixedUpdate()
     {
         if(Input.GetButtonDown("Fire1"))
         {
             Rigidbody rocketInstance;
             rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
             rocketInstance.AddForce(barrelEnd.forward * shotSpeed);
         }
     }
 }

Here's the original tutorial for reference: http://unity3d.com/learn/tutorials/modules/beginner/scripting/instantiate?playlist=17117

Any ideas what the problem is in my version, or if something else might be the cause?

Comment
Add comment
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

1 Reply

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

Answer by Aggrojag · Nov 29, 2015 at 04:24 AM

Change FixedUpdate to Update, and it should fix when they don't fire. I would imagine them double firing is something that's not in this code.

Edit: Fixed is called on certain intervals, update is not. In most scenarios Update runs more often. Anything that is physics related should generally go in fixed update, things that aren't sensitive to being called whenever possible (example being input), then use Update.

Comment
Add comment · Show 4 · 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 Bunny83 · Nov 29, 2015 at 05:05 AM 0
Share

Right, one-time-events only lasts for one frame. Since FixedUpdate doesn't necessarily run every frame you can easily miss one-time-events. FixedUpdate should only be used for applying forces over time. Even things like a jump force or your one time launch impulse should be applied in Update ins$$anonymous$$d of FixedUpdate.

avatar image Aggrojag Bunny83 · Nov 29, 2015 at 05:10 AM 0
Share

Good call on clarifying that. $$anonymous$$y wording was sloppy.

avatar image csgeorge · Nov 29, 2015 at 05:08 AM 0
Share

Changing it back to Update appears to have fixed both issues. I changed it because in Update, the projectiles will sometimes pass through objects at the velocity I wanted, which FixedUpdate resolved. But, it seems like that's the lesser of the two evils. Thanks!

avatar image Aggrojag csgeorge · Nov 29, 2015 at 10:14 PM 0
Share

I don't see how FixedUpdate would resolve that. The same amount of force will be applied regardless of where it's called. Unless force is applied continuously to the same object, this type of error will likely still be present. As far as the projectiles passing through, either it sounds as if they are moving too quickly, or objects that they are interacting with are too small. Best of luck.

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

33 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

Related Questions

Distribute terrain in zones 3 Answers

GameObject instantiation in C# static constructor 2 Answers

How to Instantiate a GameObject from a ScriptableObject piece of script? 0 Answers

Multiple Cars not working 1 Answer

Cloning Objects with Instantiate() - variables/references for added Components not stored? 3 Answers


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