Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
0
Question by Jamiemon · Jun 30, 2017 at 09:31 PM · variablestaticstatic variablenegative

Will static variables not work if negative?

I've run into a bit of a problem that nobody's even seemed to ask any similar questions to;

For the sake of simplicity I'll condense this down to just the chunks of code at fault I've got two scripts: one that detects collision & sets a static variable when it does, & a second that controls the character & changes based on the other script's static variable.

This script is called budube, it's the collision script

     static var changeMenti = -1.999;
 // a variable that will set the x momentum of the movement script if changeMenti is more than 0
     
     function Update(){
     if(wolfMove.menti == changeMenti){
         changeMenti = 0;}
 // this is just in case OnTriggerExit fails to set changeMenti back to 0
     }
     
     function OnTriggerStay2D(other : Collider2D){
         if(other.name == "springo"){
         if(gameObject.name == "rightside"){
             changeMenti = (-0.5);
     }
         if(gameObject.name == "leftside"){
             changeMenti = 0.5;
     }
         }}
 // this will set changeMenti to 0.15 or -0.15 if the left collider or right collider touches the springo respectively
 
     function OnTriggerExit2D(other : Collider2D){
         if(other.name == "springo"){
             if(gameObject.name == "leftside" || "rightside"){
                 changeMenti = 0;
          }}}


This is called wolfMove, it's {the problematic part of} the movement script

     static var menti = 0; // x momentum
     static var menty = 0; // y momentum
     
     function Update (){
     
     // this sets the x momentum to be the static variable from the budube script
     if(budube.changeMenti > 0){
             menti = budube.changeMenti;
 }
     
     transform.position.x = transform.position.x +  menti; // moves x by menti
     transform.position.y = transform.position.y + menty; // move y by menty
         }


The problem I'm running into is that the script runs just fine if changeMenti is 0.15, it sets back to 0 & makes the wolfMove script act like it bounced against a spring like it's supposed to. But when it sets changeMenti to -0.15, wolfMove won't even acknowledge it. I've tested all parts of both these scripts individually & found that changeMenti won't do anything to wolfMove if it's negative. It acts like the number is still 0, even if I set changeMenti manually. Does anyone have any clue what's going on? I literally have no context as to why this is happening considering I've used plenty of other static variables that work just fine with negative numbers, what's different here?

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 Habitablaba · Jun 30, 2017 at 09:43 PM 0
Share

you are only setting menit if your static variable is > 0, so you're specifically ignoring if it is -0.15.

change your code to

      if(budube.change$$anonymous$$enti !=  0){
              menti = budube.change$$anonymous$$enti;
     }

Amd report back

1 Reply

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

Answer by nuonical · Jun 30, 2017 at 09:55 PM

Your Update() function doesn't allow the value to be set below 0 :

 if(budube.changeMenti > 0){
              menti = budube.changeMenti;
  }

 transform.position.x = transform.position.x +  menti; // moves x by menti
 transform.position.y = transform.position.y + menty; // move y by menty

You can set this if your change is not 0. Additionally, only set the position if not 0 :

 if(budube.changeMenti != 0){
     menti = budube.changeMenti;

     transform.position.x = transform.position.x +  menti; // moves x by menti
     transform.position.y = transform.position.y + menty; // move y by menty
  }

Lastly, I can't see any reason you would need to use static variables. This will cause unexpected behavior if you have more than one of these objects. Instead, use a local variable. See this answer for more information.

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 Jamiemon · Jun 30, 2017 at 11:10 PM 0
Share

Sweet, thanks ^-^ I dunno how I overlooked that Will both static & public variables allow other scripts to reference that variable from another script?

avatar image tanoshimi · Jul 01, 2017 at 06:59 AM 0
Share

@jamiemon static variables are not to allow you to reference variables from other scripts. That's what GetComponent is for. You should hardly ever use static variables for any objects in your game other than maybe a few "global" manager objects (a save manager or audio manager, for example).

Please see the following for the correct way to reference scripts https://unity3d.com/learn/tutorials/topics/scripting/getcomponent

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

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

Related Questions

How to make variable accessible by other scripts but not visible in editor? 1 Answer

Static Variable Problem 1 Answer

Public and Static Variables 2 Answers

Is it possible to show Static Variables in the Inspector? 10 Answers

Setting static variables in Editor script 3 Answers


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