Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 micky2171 · Sep 29, 2014 at 06:22 PM · gungunfire

Gun is not shooting? Please Help!

My gun script for my multiplayer game does not work? is there something that i am doing wrong? Code: using UnityEngine; using System.Collections;

 public class Shoot : MonoBehaviour {
     
     public GameObject bullet_prefab;
     float bulletImpulse = 50f;
     float delay;
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void FixedUpdate ()
     {
         if(Input.GetButtonDown("Fire1") && delay <= Time.time)
         {
             Camera cam = Camera.main;
             GameObject thebullet = (GameObject)Instantiate(bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);
             thebullet.rigidbody.AddForce(cam.transform.forward * bulletImpulse, ForceMode.Impulse);
             delay = Time.time + 1.0f; //change 1.0f to the delay you want
         }
     }
     
     void OnGUI(){
         GUI.Box(new Rect(Screen.width/2,Screen.height/2, 10, 10), "");
     }
 }

Please Help!

Thanks, Micky2171

EDIT: UPDATED SCRIPT:

 using UnityEngine;
 using System.Collections;
 
 public class Shoot : MonoBehaviour {
     
     public GameObject bullet_prefab;
     float bulletImpulse = 50f;
     float delay = 0    ;
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void FixedUpdate ()
     {
         if(Input.GetButtonDown("Fire1") && delay <= Time.time)
         {
             Camera cam = Camera.main;
             GameObject thebullet = Instantiate(bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);
             thebullet.rigidbody.AddForce(cam.transform.forward * bulletImpulse, ForceMode.Impulse);
             delay = Time.time + 1.0f; //change 1.0f to the delay you want
         }
     }
     
     void OnGUI(){
         GUI.Box(new Rect(Screen.width/2,Screen.height/2, 10, 10), "");
     }
 }


Comment
Add comment · Show 7
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 micky2171 · Sep 29, 2014 at 07:34 PM 0
Share

Nothing except from an error - There is no shoot - If I remove the delay, nothing changes, I still get the same error as before.

avatar image Nomabond · Sep 29, 2014 at 11:20 PM 0
Share

I moved the Camera cam; to an instance member and initialized it in the Start() method, and added a cast "as GameObject" to the Instantiate method. I also changed your FixedUpdate() to Update() which was my initial recommendation. Still at work, so I haven't run this in Unity yet, but I will be home in about an hour.

 using UnityEngine;
    using System.Collections;
      
     public class Shoot : $$anonymous$$onoBehaviour {
      
         public GameObject bullet_prefab;
         float bulletImpulse = 50f;
         float delay = 0;
         Camera cam;
      
         // Use this for initialization
         void Start () {
             cam = Camera.main;
         }
      
         // Update is called once per frame
         void Update ()
         {
             if(Input.GetButtonDown("Fire1") && delay <= Time.time)
             {
                
                 GameObject thebullet = Instantiate(bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation) as GameObject;
                 thebullet.rigidbody.AddForce(cam.transform.forward * bulletImpulse, Force$$anonymous$$ode.Impulse);
                 delay = Time.time + 1.0f; //change 1.0f to the delay you want
             }
         }
      
         void OnGUI(){
             GUI.Box(new Rect(Screen.width/2,Screen.height/2, 10, 10), "");
         }
     }

avatar image micky2171 · Sep 29, 2014 at 11:25 PM 0
Share

It has removed the WAssets/Scripts/FP_Shooting.cs(20,76): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)" error but the first error is still there and there is no bullet co$$anonymous$$g out of the gun. Would it be any better if I sent you the whole unity file and then you can fix it and send it back to me?

avatar image Nomabond · Sep 29, 2014 at 11:50 PM 0
Share

That error is co$$anonymous$$g from a different script right?

 NullReferenceException: Object reference not set to an instance of an object FPShooting.FixedUpdate () (at Assets/Scripts/**FPShooting.cs**:19)

Is co$$anonymous$$g from your FPShooting script. If you're creating new scripts with the similar code make sure you're removing the other scripts from your scene objects when testing. Also can you post the code from the script that's getting the error?

avatar image micky2171 · Sep 29, 2014 at 11:59 PM 0
Share

Sorry, i tried to copy the script. Here is the real error:

NullReferenceException: Object reference not set to an instance of an object Shoot.FixedUpdate () (at Assets/Scripts/Shoot.cs:21)

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ThomLaurent · Sep 29, 2014 at 06:51 PM

For me the only thing wrong in your code is you don't initialize your delay, so your script doesn't go inside of your if statement because delay runs a random number that nobody knows in advance.

To fix it, just float delay = 0; at line 5

It should work now :)

Comment
Add comment · Show 5 · 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 micky2171 · Sep 29, 2014 at 07:20 PM 0
Share

Ii still get this error:

NullReferenceException: Object reference not set to an instance of an object FP_Shooting.FixedUpdate () (at Assets/Scripts/FP_Shooting.cs:19)

Is there anything else I can try?

avatar image Nomabond · Sep 29, 2014 at 07:46 PM 0
Share

Have you for sure added a GameObject into the bullet_prefab?

avatar image micky2171 · Sep 29, 2014 at 08:01 PM 0
Share

yes, i added the bullet to the bullet_prefab. The bullet is called "Bullet" if that helps any!

avatar image Nomabond · Sep 29, 2014 at 09:07 PM 0
Share

I will have to work on this a little bit more when I get home to my game workstation.. I'm at work now. But one thing I would suggest trying for now is removing the cast to GameObject in line 19.

So :

 GameObject thebullet = (GameObject)Instantiate(bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);

Should become:

 GameObject thebullet = Instantiate(bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation);


If this doesn't fix it, I will find the fix when I get home if someone else hasn't by then.

avatar image micky2171 · Sep 29, 2014 at 09:31 PM 0
Share

This script just stops the game from running and brings this into the console:

Assets/Scripts/FP_Shooting.cs(20,76): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

avatar image
0

Answer by Nomabond · Sep 29, 2014 at 06:54 PM

Its generally a better idea to call the Input methods from the Update() method rather than the FixedUpdate(). Change your FixedUpdate() method to Update() and see if your gun fires.

I believe that Input is actually broadcasted per-frame, so when the FixedUpdate() is behind and needs to catch up by firing multiple times, the Input check will fail.

Edit: To include the modified script that I've tested and is working. You will want to work on destroying the instantiated objects though. :)

 using UnityEngine;
    using System.Collections;
 
     public class Shoot : MonoBehaviour {
 
         public GameObject bullet_prefab;
         float bulletImpulse = 50f;
         float delay = 0;
         Camera cam;
 
         // Use this for initialization
         void Start () {
             cam = Camera.main;
         }
 
         // Update is called once per frame
         void Update ()
         {
             if(Input.GetButtonDown("Fire1") && delay <= Time.time)
             {
 
                 GameObject thebullet = Instantiate(bullet_prefab, cam.transform.position + cam.transform.forward, cam.transform.rotation) as GameObject;
                 thebullet.rigidbody.AddForce(cam.transform.forward * bulletImpulse, ForceMode.Impulse);
                 delay = Time.time + 1.0f; //change 1.0f to the delay you want
             }
         }
 
         void OnGUI(){
             GUI.Box(new Rect(Screen.width/2,Screen.height/2, 10, 10), "");
         }
     }

Additional Resources:

http://answers.unity3d.com/questions/10993/whats-the-difference-between-update-and-fixedupdat.html

http://stackoverflow.com/questions/19259097/is-it-really-wrong-to-use-input-getkey-on-fixedupdate

Comment
Add comment · Show 6 · 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 micky2171 · Sep 29, 2014 at 07:19 PM 0
Share

Can you please put that in an easier way to fix my code? I don't really want to read a full paragraph on how to fix it.

avatar image micky2171 · Sep 30, 2014 at 04:43 PM 0
Share

do i need to add the bullet to the game or leave it in the assets area and then put it on the player script. If i have to add the bullet to the game, how can i add it to the player? Your script did not work. Should i send you the whole project and see if you can fix it for me???

avatar image Nomabond · Sep 30, 2014 at 05:50 PM 0
Share

This script definitely works. I've tried it multiple times on different scenes and it shot no problem.

You need to add your prefab to the script in the public GameObject bullet_prefab; area within the inspector.

avatar image micky2171 · Sep 30, 2014 at 06:02 PM 0
Share

does it have anything to do with the fact that my Player is not put into the game until you click "Host Game" or "Join Game" (Remember: it is a multiplayer game.)

: The script that you sent me does not work for me. Please Help $$anonymous$$e!

avatar image Nomabond · Sep 30, 2014 at 06:29 PM 0
Share

I don't know how you're instantiating players on network join, but you will need to make sure that your player prefab has the correct script with the bullet prefab added to the script BEFORE you instantiate on the network.

Show more comments
avatar image
0

Answer by Addyarb · Sep 29, 2014 at 09:44 PM

My suggestion would be to take line 19 out and add it in the update function of a script attached to your bullet prefab. Also, take (GameObject) out of the script on line 18.

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 micky2171 · Sep 29, 2014 at 09:48 PM 0
Share

when i take the (GameObject) out of line 18, i get this error:

Assets/Scripts/FP_Shooting.cs(20,76): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

Any reason why?

avatar image Nomabond · Sep 29, 2014 at 10:25 PM 0
Share

Can you update your question with the current full script, so I can match up line numbers for your error?

avatar image micky2171 · Sep 29, 2014 at 10:35 PM 0
Share

Look at the EDIT in the question - that is the updated version that you asked for.

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Make bullet launch at center of screen 2 Answers

Help with gun scripting!? 1 Answer

Make player unable to shoot when reloading 3 Answers

Gun movement 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