- Home /
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), "");
}
}
Nothing except from an error - There is no shoot - If I remove the delay, nothing changes, I still get the same error as before.
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), "");
}
}
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?
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?
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)
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 :)
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?
Have you for sure added a GameObject into the bullet_prefab?
yes, i added the bullet to the bullet_prefab. The bullet is called "Bullet" if that helps any!
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.
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?)
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
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.
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???
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.
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!
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.
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.
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?
Can you update your question with the current full script, so I can match up line numbers for your error?
Look at the EDIT in the question - that is the updated version that you asked for.
Your answer
Follow this Question
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