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 coming 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 coming 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 coming 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)
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?
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?)
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:
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 Me!
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.
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?
So what happens? Does it not shoot? does it shoot but the bullet won't show on the other player? If you remove the delay from the check, what happens?
– screenname_takenNothing except from an error - There is no shoot - If I remove the delay, nothing changes, I still get the same error as before.
– micky2171It 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 coming 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?
– micky2171That error is coming 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 coming 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?
– NomabondSorry, 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)
– micky2171