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
0
Question by Kleptomaniac · Apr 13, 2012 at 12:50 PM · functionstaticreturn0member variables

Member variables returning to 0

Hi there,

I'm trying to create some classes and static functions which will return values after a certain period of time for me. I've managed to get it working, however some of the variables aren't necessarily defined by the arguments passed through to the function, and so I need to use static vars for them. While this works, it means that I'm unable to ever call the functions more than once because these variables only ever have one instance. What I'm trying to do instead is to have these variables declared as member variables for the functions. This is my code:

 static function Intf (val : int, time : float) : int {
     var timer : float;
     timer += Time.deltaTime;
     timer = Mathf.Clamp(timer, 0, time);
     if (timer == time) {
         return inst;
     } else {
         inst = val;
         return val;
     }
     return;
 }

However, unfortunately, timer continuously returns to 0 because of the way it is declared. I know that this is because the compiler is continuously redeclaring the variable with a value of 0 at this line: var timer : float; but I'm not sure of the best way in which to counter this effect.

Any help on better ways to do these sorts of functions, etc. from people who've successfully done them before would be much appreciated. After all, how do we ever learn if not through our mistakes?

Sorry if I wasn't clear enough!

Thanks very much,

Klep :)

Comment
Add comment · Show 16
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 fafase · Apr 13, 2012 at 01:03 PM 0
Share

What if you make timer global and pass it to the function as parameter.

avatar image Lttldude · Apr 13, 2012 at 01:07 PM 0
Share

I may not fully understand and be way off on my answer and I apologize, but I defined the timer as a static variable in a class. Then ran your function in an object's update function and the conditional was true after the designated amount of time. Is that what you were trying to do?

avatar image Kleptomaniac · Apr 13, 2012 at 01:10 PM 0
Share

You mean like so?

 static var timer : float;
 
 static function Intf (val : int, time : float) : int {
     timer += Time.deltaTime;
     timer = $$anonymous$$athf.Clamp(timer, 0, time);
     if (timer == time) {
        return inst;
     } else {
        inst = val;
        return val;
     }
     return;
 }

Yes, I have already tried this, and despite it working, it won't allow me to do things like this in say another script:

 var foo : int;
 var foobar : int;
 
 function Update () {
 
 foo = Log.Intf(Time.time, 9);
 foobar = Log.Intf(Time.time, 40);
 
 }

This will cause either one or both of those calls to screw up. Even if this isn't a great example of this sort of static function, I'm trying to start small to learn how to do them properly. :)

avatar image fafase · Apr 13, 2012 at 01:21 PM 1
Share

Why do you need it static? Why don't you make a normal function and call with a message?

avatar image Lttldude · Apr 13, 2012 at 01:24 PM 0
Share

Exactly.

I see what you are trying to do. $$anonymous$$aybe you can add a third parameter like an id number. So each time you run a new function, it will have a new id number. The timer static variable could become an array, and when the I d number is greater than the length $$anonymous$$us 1 of the array then you can add an entry. $$anonymous$$aybe?

Show more comments

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get a public variable to the function. 3 Answers

Access a class on another script? 1 Answer

C# Return Type Error? 1 Answer

GUI calling function with return value. I dont know why this doesnt work. 0 Answers

Input.GetAxis returns a value, whilst CrosPlatformInputManager.GetAxis dosn't. 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