• 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
Question by domwhu777 · Apr 20, 2013 at 02:30 PM · javascriptunity4executableexe

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.

Comment

People who like this

0 Show 4
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 Raydaq · Apr 20, 2013 at 02:57 PM 0
Share

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.

avatar image domwhu777 · Apr 20, 2013 at 03:23 PM 0
Share

Yeah, that's not the problem with my game because when I shoot my ammo variable is still the same.

avatar image xandermacleod · Apr 20, 2013 at 04:00 PM 0
Share

My 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

avatar image domwhu777 · Apr 20, 2013 at 09:15 PM 0
Share

doesn't work :(

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

How to solve the issue an executable file that shows a blank black/blue screen when you try to open it? 0 Answers

A node in a childnode? 1 Answer

(Programming languages) Can Javascript/Unityscript be executed as standalone ? 1 Answer

Control Rotation via Touchscreen on iPad? 1 Answer

My weapon does no damage? 0 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