Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

static class not functioning well

Discussion in 'Scripting' started by YMbrothers, Dec 13, 2015.

  1. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Plz, everyone who saw this thread at least give a reply...

    I got a code like this:
    (btw, it's Javascript, idk why the code thing said it's C# -,-)
    Code (csharp):
    1.  
    2. #pragma strict
    3. public class Charge{
    4. public static var charge_time :int=2;
    5. function Update()
    6. {
    7. print(charge_time);
    8. if(Input.GetKey(KeyCode.Space)){
    9. charge_time --;
    10. }
    11. if(Input.GetKeyUp(KeyCode.Space)){
    12. charge_time =2;}
    13. }
    14. }
    15.  
    The problem is: I tried this thing, and it seems that the variabe "charge_time" didn't change its value, this thing didn't ever print anything at all,

    I tried the print in other scripts, the script prints it successfully, but the value appeared to be remain unchanged. I think the problem is that
    Code (csharp):
    1.  
    2. if(Input.GetKey(KeyCode.Space)){
    3. charge_time --;}
    4.  
    is not functioning nor functioning in a loop.

    So, I wish for an answer, I have to do this b4 the end of this year...
     
    Last edited: Dec 13, 2015
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. function Update()
    3. {
    4. print(charge_time);
    5. ..
    6. }
    7.  
    this should be printing every frame. If you aren't getting that a few things come to mind.

    You're console is on "collapsed" so you aren't getting a new line for each output, you just get a increased count on the right of the console.

    The script isn't attached to anything in the hierarchy.

    The script is attached, but disabled.

    You've got the unityscript syntax wrong... but I'm c# peep so I'm missing some nuance to it... join the dark side, we have cookies* :confused::p





    (*had cookies, we ate them, join the dark side so we can send the new guy to the shop to get more... :) )
     
  3. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    This is a class script, so it doesn't attached to anything,
    does that mean I have to at least attach to an empty game object?

    Edit: I got alot of cookies in Javascript because Minecraft provided me cookies XD
     
  4. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    The Update() function derives from Monobehaviour. Static classes are unable to inherit from Monobehaviour and so do not have access to the Update() function. Normally static classes inherit from Object. We need to see what your static class is inheriting. Could you please post the entire script?

    There ARE ways to get around this, but it'll take cheeky fixes like:

    Code (CSharp):
    1. // From within an instanced subclass of Monobehaviour
    2. void Update()
    3. {
    4.     YourStaticClass.Update();
    5. }
    Or extending the editor and using on of their constantly updating methods.

    Edit: Oops just realised that IS your whole script! It's not inheriting from anything, so there WILL be no Update() function being called. You'll have to call that like I mentioned above.
     
    Last edited: Dec 15, 2015
    LeftyRighty likes this.
  5. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Does that mean I only need to delete the Update function
    and attach this script to a gameObject
    and it'll be looped?

    Because I want this script to detect the keyboard(as you've seen)
    but it doesn't move at all,
    it didn't even print anything when I start this script.
    So I think it didn't even activate.

    Sorry to be stupid, I just used Unity for about a month only.
     
    Nigey likes this.
  6. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    OOoooh you've only started a month ago? You're not being stupid. In fact because me and Lefty normally use Javascript I think we're getting confused here (I certainly was lol).

    Okay it's really simple :). Basically drag and drop your script into an existing GameObject in the Hierarchy (in other words any object in your scene). The Update() function will loop automatically. The class isn't static, it's only your variable which is static, so that'll work just fine.

    I've just put this on a GameObject, and it worked just fine:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. public static var charge_time : int=2;
    4.  
    5.     function Update()
    6.     {
    7.         print(charge_time);
    8.  
    9.         if(Input.GetKey(KeyCode.Space))
    10.         {
    11.             charge_time --;
    12.         }
    13.  
    14.         if(Input.GetKeyUp(KeyCode.Space))
    15.         {
    16.             charge_time = 2;
    17.         }
    18.     }
    All I needed to do to get it working was remove the 'class Charge{ }' bit.
     
  7. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Thnx for a long reply and an edited script (LeftyRighty uses C#)

    So, is this script going to be used for all script?
    If it is, can you tell me how to use it?

    Do I just use it as if it's exist as a variable for all scripts?
    Or I have to create another variable to store the static variable?
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    <<Insert generic plug for C# over UnityScript>>

    UnityScript does some odd things automatically with classing. If you don't specify a class header the compiler creates one for you that inherits from MonoBehaviour and matches the file name.

    If you want to explicitly subclass it looks like this

    Code (JavaScript):
    1. public class MyClass extends MonoBehaviour {
    2.     // Some stuff
    3. }
     
    Nigey likes this.
  9. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Well, new knowledge...
    Too bad I'm using Javascript
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The code example I gave you was UnityScript/JavaScript. And it is an alternate way to solve your problem.
     
  11. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Do you mind to give me the whole code?
    I got my issued code on top.

    Thanks for it, again.
     
  12. YMbrothers

    YMbrothers

    Joined:
    Dec 7, 2015
    Posts:
    29
    Thnx guys, I finally solved it (*sobbed*)

    But, don't mind if I ask you another question:
    how to get a rigidbody's localScale?
    I know it's about Get, but Idk what the scale coded like.