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
-1
Question by jmaster2012 · Feb 13, 2013 at 01:29 AM · javascripterrorfailurefiring

EMERGENCY!!! PLEASE HELP!!!!!

Ok. Sorry, but I am REALLY desperate... my script is half working. What it is supposed to be doing is turning a turret to look at my character, and firing one fireball per second or something, and it is looking, but it wont fire! yes I checked every possible error. For this I might need a really good coders help. I also dont mind if you request that I make a new code for this. Here is the code:

 var LookAtTarget : Transform;
 var damp = 1.0;
 var BulletPrefab : Transform;
 var savedTime;
 
 function Update()
 {
     if(LookAtTarget)
     { 
         var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
         
         transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
     
 var secs;
 secs+= Time.deltaTime;
 if(secs < 1)    
 {    
     Shoot();    
     secs=0;
         
     }
     }
 }
 function Shoot()
 {
     var bullet = Instantiate(BulletPrefab, gameObject.Find("spawnPoint1").transform.position, Quaternion.identity);
     bullet.Rigidbody.AddForce(transform.forward * 1000);
     
     
     
 }

THANKS SO MUCH FOR HELPING!!!!!

Comment
Add comment · Show 1
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 hoy_smallfry · Feb 13, 2013 at 01:35 AM 1
Share

Dude, clean up your code blocks, I'm gonna have to copy paste into my own editor to even read it. Also, please don't use a title like that. We are all here for help, but with a title like that, people dont have any idea what you're asking about until they click. Some people will just ignore your post if you do that.

What is the variable "LookAtTarget" supposed to represent? is it a state that the turret is supposed to be in? (One state being looking, the opposite being not looking)

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by hoy_smallfry · Feb 13, 2013 at 01:50 AM

Ah, I just figured it out. See, every Update call, you are creating a variable called "secs". The state of that variable will cease to exist at the end of a it's scope. Usually, you can tell what a variable scope is by looking at the first set of brackets that surround it:

 {
     var x = 10;
 
     {
         var y;
 
         // x also exists in scopes under the one it was created in
         x = 34;
 
         // y exists within these brackets, so i can do this:
         y = 5;
     }
     
     // y no longer exists here - this will cause an error:
     y = 5;
 
     // x is still alive, though.
     x = 0;
 }

This is something that most imperative programming languages like Javascript and C# do. What it means to you is that at the end of the if statement, "secs" no longer exists. Every time the Update() starts over, you have an entirely new "secs" variable that starts at 0.

So, long story short, your "secs" variable will never get to 1 because the old values of Time.deltaTime never add up.

However, if you define secs outside the Update() function, it will stay alive throughout the entirety of the script's lifespan.

Comment
Add comment · Show 2 · 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 aldonaletto · Feb 13, 2013 at 02:03 AM 0
Share

NOTE: In Unityscript, the scope of a variable declared inside a function is the entire function. It's different from C#, where a local variable only exists inside the brackets where it's defined (I made this same mistake too)

avatar image flaviusxvii · Feb 13, 2013 at 05:18 AM 0
Share

Yeah, they call that "hoisting".. yet another reason to avoid Unityscript.

avatar image
0

Answer by Proportion1 · Feb 13, 2013 at 02:19 AM

your condition that gets the thing to shoot is wrong. it should be

 if(secs > 1){
 
 Shoot();
 secs = 0;
 
 }

cos like u want it to shoot after 1 sec yea?

Comment
Add comment · 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
0

Answer by aldonaletto · Feb 13, 2013 at 01:58 AM

Have you set the field LookAtTarget? You must drag the target object to it in the Inspector, otherwise nothing will happen. You should also set the field BulletPrefab, or tons of error messages will pop up in the console view. Finally, the secs variable is incredibly useless: it's created every Update, thus every time it starts with value 0, and pass the if - you should have projectiles being created like bubbles in front of your weapon.
You could change the code to something like this:

       ...
       transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
       // replace that secs part with this:
       if (Time.time > savedTime){ // if it's time to shoot...
           Shoot(); // shoot...
           savedTime = Time.time + 1; // and define the next time to shoot
       }
    }
 }

 function Shoot(){
    ...

Also define the savedTime type as float in its declaration:

 var savedTime: float;

Anyway, pay attention to the error messages in the console view when running: many errors only appear at runtime. When a runtime error occurs, the function is aborted and all instructions after the error aren't executed.

Comment
Add comment · Show 1 · 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 jmaster2012 · Feb 13, 2013 at 08:13 PM 0
Share

Yours worked. However, I want it to shoot ONCE every second, not a lot forever.... Is there a way to do that?

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

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

Expecting ( found Update (Javascript) 1 Answer

Error Unexpected token } 1 Answer

"Expecting ), found ';'" and "';' expected. Insert a semicolon at the end" 1 Answer

Need Help Understanding Script! (Javascript) 1 Answer

OnMouseEnter Error 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