• 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 Piflik · Sep 18, 2011 at 11:27 AM · javascriptfunction

Function not reseting variable passed to it

I have written a simple function to regulate the rate of fire for a ranged weapon. It works as long as I use a single, predeclared variable as a counter between two bursts, but as soon as I use two separate variables (for primary and secondary attack), it stops working. The variables are still used in an if statement, but it is not reset to zero later.

This is the function:

 function fire(projectile : Transform, waitProj : int){                                //FIRE!!!!
     if(waitProj >= projectile.GetComponent(Projectile).rateOfFire) {
         waitRate++;                                                                    //increase updates since last projectile
         if(waitRate >= Mathf.Floor(statics.turnDuration / projectile.GetComponent(Projectile).projSpeed)) {
             var tempProj = Instantiate(projectile, lookWP + Vector3(0, 5, 0), Quaternion.LookRotation(lookWP - oldWP));
             tempProj.GetComponent(Projectile).owner = "player";
             waitRate = 0;
         }
         if(statics.currentDuration == statics.turnDuration) {
             waitProj = 0;
             waitRate = 0;
         }
     }
 }

And this is the call (in FixedUpdate()):

 fire(primAtt, waitPrim);

'waitPrim' is increased outside of the function and it counts the number of turns passed since the last burst. 'waitRate' counbts the number of updates since the last projectile.

When I use a single variable (waitProj) for both fire modes, it is set to 0 in the function, but I want the two fire modes to be independent of each other, so I declared two variables (waitPrim and waitSec) and pass them to the function instead, but they are not set to 0.

The variables are passed correctly, because the weapon is not fired, when I start the game, until the counter reaches the rateOfFire variable I set in the projectile, but then it is fired continuously, without pause, since the counter keeps counting up without ever being reset.

Any ideas, what I did wrong? Probably something fundamental...

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Piflik · Sep 18, 2011 at 01:14 PM

After thinking about it some more, I think I know what the problem is, and as I thought, it is quite fundamental.

A function cannot change the variable passed to it, only the local variable it used within its own scope. If I want to get the changed value from a function, I need to either use predeclared variables or use 'return'. Kinda makes sense...

Can someone, who has some more programming experience than me (i.e. more than two weeks), confirm this?

Comment

People who like this

0 Show 0 · 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

Answer by Peter G · Sep 18, 2011 at 03:58 PM

It looks like you need to pass waitProj as a ref. the ref keyword causes changes to the parameter to affect the original variable. You can pass a reference type or a value type by reference (ref).

 void fire ( Transform projectile , ref int waitProj) {
     //.....
     //that will make this line work as expected.
     waitProj = 0;
     //......
 }
Comment

People who like this

0 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 Piflik · Sep 18, 2011 at 04:05 PM 0
Share

Is this possible in Javascript, too, or would I have to rewrite my script in C#?

I got it working now by adding 'return(waitProj);' and calling the function by 'waitPrim = fire(primAtt, waitPrim);', but your solution looks more elegant.

avatar image Peter G · Sep 18, 2011 at 10:44 PM 0
Share

A quick test suggests you can't use the ref keyword in js. But you can write the function in C#, then use it in js with the same effect if that makes sense. Ref will affect variables passed from a js script into a C# function, it just can't be declared in a js script.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Incremental game need help 1 Answer

A node in a childnode? 1 Answer

Call function from other script 0 Answers

Assets/Editor/UpdateTreeColors.js(13,17): BCE0031: Language feature not implemented: UnityEditor. 1 Answer

How can I make a function wait before it starts running again? 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