Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by AMDAndrew · Sep 22, 2012 at 07:54 AM · runningstatic variables

Static Variables

At first, I have read all the static variables post on forum and questions.

I am using 2 scripts - 1 that is Running and 1 that is Character Motion

In the first script I have declared a static variable called running.

 #pragma strict
 static var running : boolean;
 

In the second script I'm trying to call the variable running from the script but I don't know why it doesn't work

 var running;
 
 running = Running.running;

And I have also tried with getComponent and I got the same result.

running is suposed to be a boolean that when activated max Speed of the character will increase with 2

Comment
Add comment · Show 8
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 whydoidoit · Sep 22, 2012 at 07:58 AM 1
Share

It would be a good idea to define running as a boolean and also to put #pragma strict at the top of all of your source files - this forces you to be clear in your variable declarations and helps fix some hard to spot bugs. It's necessary for platforms like iPhone.

static var running : boolean;

Now what error do you get when you try to access the running variable - because it looks ok apart from the declaration needing the type.

avatar image AMDAndrew · Sep 22, 2012 at 08:24 AM 0
Share

$$anonymous$$ indentifier "Running"

in Character motor script

avatar image whydoidoit · Sep 22, 2012 at 08:26 AM 0
Share

And the script is called Running (with a capital R?)

avatar image AMDAndrew · Sep 22, 2012 at 08:30 AM 0
Share

http://s8.postimage.org/la6d27hat/Untitled.png

I have also attached both scripts to Player

avatar image whydoidoit · Sep 22, 2012 at 08:31 AM 0
Share

Can you post the other script? Does it have anything in it called Running that could be hiding the Running class?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Fattie · Sep 22, 2012 at 08:55 AM

AMD dude, this ..

alt text

gives me the moral authority to say that the "static" path you have stumbled on to is a false path.

So, completely forget about "static". Don't use it.

If you simply post all your code clearly, someone will easily find the problem and be happy to do so!!


A) get rid of the static

B) have a variable called "running" IN ONE SCRIPT ONLY.

DO NOT HAVE A VARIABLE CALLED RUNNING IN THE OTHER "SECOND" SCRIPT.

C) in the second script do this in awake

 var teste:GameObject;
 teste = GameObject.FInd("you type this");
 if ( teste == null )
     Debug.Log("Something is messed up");
 else
     Debug.Log("everything is peachy");

D) test that. tell us exactly what happens

E) in the second script add code like

 var otherScriptName:OtherScriptName;
 otherScriptName = teste.GetComponent(OtherScriptName);

now in that second script, only ever use...

otherScriptName.running

again, see point 2 .. get rid of "running" from the second script, and from now on use only "otherScriptName.running"

F) if still having problems, delete the two paste bins, repaste everythign, and let us know


meurs.jpg (315.9 kB)
Comment
Add comment · Show 8 · 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 whydoidoit · Sep 22, 2012 at 08:56 AM 0
Share

Yeah that's true - though if it can't find Running - then I doubt GetComponent(Running) will work either... Interesting.

avatar image AMDAndrew · Sep 22, 2012 at 09:05 AM 0
Share

I want a static variable because I have 2 scripts and I want to use the boolean "running" from the first scrip in Character $$anonymous$$otor script as for the player when he presses shift var running = true and if running = true the variables maxForwardSpeed and the others 2 will increase with 2. And I want to separate the running script from Character $$anonymous$$otor

avatar image AMDAndrew · Sep 22, 2012 at 09:35 AM 0
Share

http://pastebin.com/pB3wegxi

http://pastebin.com/6q3Wap$$anonymous$$G

avatar image AMDAndrew · Sep 22, 2012 at 10:55 AM 0
Share

thx it worked

avatar image Fattie · Sep 22, 2012 at 11:10 AM 0
Share

$$anonymous$$agnificent!

I urge you to please not use Static variables until you are totally confident with, err, parametric polymorphism. Add, err, memorise all of the book seen in this screen shot !

http://answers.unity3d.com/questions/265828/if-you-destroy-a-list-of-class-objects.html

do not hesitate to ask more questions here but first erase all Static mentions. and don't use globals! :) bottoms up

Show more comments
avatar image
4

Answer by aldonaletto · Sep 22, 2012 at 09:53 AM

The problem here is the compilation order. Unity compiles scripts in 4 consecutive steps, according to the folders where they are stored (see Script Compilation, in the docs). Scripts compiled in some step can't see the others that will be compiled in subsequent steps. Since CharacterMotor is in Standard Assets (first step), it can't see your script (third step, apparently).
You can move the running declaration to CharacterMotor, like this:

  • CharacterMotor.js:


  static var running: boolean = false;
  ...
  if (running == true){
    maxForwardSpeed = 5 ; 
    maxSidewaysSpeed = 5 ;
    ...
  • Running.js:


  ...
  function Update(){
    // assign the button state directly to running (you don't need that doubled if):
    CharacterMotor.running = Input.GetButtonDown("Run");
    if (run == true){ //
      ...

NOTE: A more elegant and bullet-proof alternative would be to not make running static - being static, all characters that use CharacterMotor will run when you press Run! The non static alternative would be like this:

  • CharacterMotor.js:


  public var running: boolean = false;
  ...
  if (running == true){
    maxForwardSpeed = 5 ; 
    maxSidewaysSpeed = 5 ;
    ...
  • Running.js:

... private var chMotor: CharacterMotor; // reference to CharacterMotor

function Start(){ // get the CharacterMotor assigned to this player: chMotor = GetComponent(CharacterMotor); }

function Update(){ chMotor.running = Input.GetButtonDown("Run"); if (run == true){ // ...

Comment
Add comment · Show 5 · 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 whydoidoit · Sep 22, 2012 at 10:13 AM 0
Share

Good point!

avatar image Fattie · Sep 22, 2012 at 10:21 AM 0
Share

ouch guys. the OP is (A) struggling with basic variable and GetComponent use, and has (B) accidentally started using static ... could be a further complication to introduce compilation order! :O

avatar image AMDAndrew · Sep 22, 2012 at 11:23 AM 0
Share

bro I think you're kind of genius

avatar image Fattie · Sep 22, 2012 at 11:29 AM 1
Share

I sure hope you mean $$anonymous$$ike - i'm the village drunk. I mean I just vomited on myself, seriously. Take it easy now !

always remember, program$$anonymous$$g is the hardest human challenge that has ever existed !! failure is the only option!

always remember that no software system ever - that's No software system .. Ever .. has ever been completed to a working state. if you're tackling program$$anonymous$$g you're beyond el33t. you've put yourself in the hardest human place. even the lamest lame-ass question is a masterpiece]

guys like Aldo and $$anonymous$$ike can see in to the Rubik's cube. that is way difficult - VERY rare

and now, time for chicken.

avatar image Lo0NuhtiK · Apr 04, 2014 at 09:49 PM 0
Share
  • @Fattie for the classic post. I got a few laughs out of this page xD

...and... bump for a new-ish person having some problems with static's.

http://answers.unity3d.com/questions/680545/accsesing-variables-from-another-script-but-they-h.html

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

15 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 avatar image avatar image avatar image

Related Questions

Is Running/Playing flag? 2 Answers

I cant seem to add a cooldown to this script 1 Answer

Game not working properly 0 Answers

How do I check for errors in Visual Studio 1 Answer

Starting and stoping running script with C# 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