- Home /
Works when testing in unity but not as a built project
My shooting script works well but it dosen't work at all when i build the project and it runs as a .exe
This is my script: #pragma strict
/* This is the ammo and firing script
It fires a projectile untill the ammo runs out.
This must be attached to an empty game object infront
of the gun it's being used on
*/
//Variables
// Bullet or what is going to be shot
var Projectile : Transform;
// Fire rate of shooting (Evrey # (0.3 by defualt) seconds, 1 shot is fired)
static var fireRate : float = 0.3;
//Time to start shooting
private var nextFire : float = 2.0;
//How much ammo the player has
static var Ammo: int = 30;
// Sound Effect
var gunfire : AudioClip;
var reload : AudioClip;
// Booleans
// Is the Player out of ammo?
var empty: boolean = false;
function Update () {
// As Long as the time the game starts and the fire tate = nextFire and is less than Time.time, it'll fire.
if(Input.GetMouseButtonDown(0))
{
// If ammo is more than 0 run function Fire
if(Ammo > 0)
{
Fire();
}
// Otherwise return
else
{
return;
}
}
if(Input.GetKeyDown("r"))
// If the R key is pressed run function release
if(empty)
{
Reload();
}
// If the ammo is less than or equal to 0 set empty to true
if(Ammo <= 0)
{
empty = true;
}
// Otherwise set it to false
else
{
empty = false;
}
}
function Fire()
{
/*
Play gunfire audio, add to the next fire time,
create the bullet and launch it by adding force
to it's rigidbody, and then substract 1 to the ammo count.
For it to work, whatever the script is attached to
needs and audio script component and the bullet needs a
rigidbody :) :) :)
*/
audio.PlayOneShot(gunfire);
nextFire = Time.time + fireRate;
var shot = Instantiate(Projectile,transform.position, Quaternion.identity);
shot.rigidbody.AddForce (transform.forward * 3000);
Ammo--;
}
function Reload()
{
// Reset Ammo to 30
audio.PlayOneShot(reload);
Ammo = 30;
}
function OnGUI()
{
// Display the status
GUI.Box (Rect(Screen.width - 90, 20, 100, 80), "");
GUI.Label (Rect (Screen.width - 90, 35,90,20), "Ammo: " + Ammo);
GUI.Label (Rect (Screen.width - 90, 20, 90, 20), "Health: " + PlayerHealth.curHealth);
//If it's empty, show the following display
if(empty)
{
GUI.contentColor = Color.red;
GUI.Label (Rect (Screen.width - 90, 50, 90, 20), "Reload");
}
}
// Allows the OnGUI function to appear in the editor mode
@script ExecuteInEditMode()
Note: If you help me fix this problem you can ask for your name in the credits and I may put your name in their if your solution worked.
I had a similar thing happen today and found out that my prefab im instantiating is not connected after my build. If its the same for you it would be your bullet prefab is no longer connected to var Projectile : Transform; I haven't solved the issue yet, but if I do i'll let you know what i did.
Yeah, that's not the problem with my game because when I shoot my ammo variable is still the same.
$$anonymous$$y guess is to do with Line 88 - PlayerHealth.curHealth I assume this is meant to be a Singleton script? I'm more of a C# person, so I'm not sure if that needs to be something like " PlayerHealth.Instance.curHealth " or not. Alternatively, that script might be trying to reference a library that isn't available on the device where ur running the .exe - P.S. If it does turn out to be the solution, Id be more than happy to go in some credits! :P