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 ciwiaf · Apr 18, 2015 at 12:23 PM · script.valuepassingpowerbar

Instantly pass other script's variable value to another.

Hi, I'm trying to pass a variable value from one script to another, but it seems that it's passing it too late or I may have other problems. I'm still learning Unity and C# so forgive my ignorance :). Anyway, I'm trying to make one code that will start a "power meter" when you press and hold Mouse1, and another code that will make the character jump when you release Mouse1, depending on the value of the "power meter".

Here's the code attached to my power meter.

     void Update () {
 
         if (Input.GetMouseButtonDown(0) && (startMeter == 0)) {
             barStart = true;
             startMeter = 1;
         }
         if (Input.GetMouseButtonUp(0) && (startJump == 0) && barStart) {
             barStart = false;
             startJump = 1;
         }
         if(barStart && !maxJump){
             jumpPower += Time.deltaTime * barSpeed;
             jumpPower = Mathf.Clamp(jumpPower, minWidth, fullWidth);
             powerBar.pixelInset = new Rect (transform.localPosition.x, transform.localPosition.y, 20, jumpPower);
             if(jumpPower == 256){
                 maxJump = true;
             }
         }
         if(barStart && maxJump){
             jumpPower -= Time.deltaTime * barSpeed;
             jumpPower = Mathf.Clamp(jumpPower, 0, fullWidth);
             powerBar.pixelInset = new Rect (transform.localPosition.x, transform.localPosition.y, 20, jumpPower);
             if(jumpPower == 0){
                 maxJump = false;
             }
         }
         
     }


And here's the other one attached to the player:

 public PowerMeter jumpForce;
 
     void Update () {
 
         if (Input.GetMouseButtonUp(0) && grounded && (jumpForce.startJump == 1)) {
             GetComponent<Rigidbody2D>().velocity = new Vector3(0,0,0);
             GetComponent<Rigidbody2D>().AddForce (new Vector2(-3,(jumpForce.jumpPower/7)), ForceMode2D.Impulse);
             GetComponent<Rigidbody2D>().mass = 2;
             Destroy(GameObject.FindWithTag("PowerMeter"));
             //jumpForce.startJump = 0;
         }


whenever I let go of the Mouse1, the player would ignore the value set by the power meter (jumpPower variable). but when I press and release mouse1 again, the player would finally jump based on the value in the jumpPower.

I'm stuck, any help is appreciated.

Thanks :)

Comment
Add comment
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
1
Best Answer

Answer by vargata · Apr 18, 2015 at 02:36 PM

it would be better to call a jump function on the player from the powermeter's script instead of using the update twice. its more reliable and you can pass parameters.

 var playerObject : gameObject
 
 function Update(){
   var playerScript = playerObject.GetComponent(jumpScript);
 
   if (Input.GetMouseButtonUp(0) && (startJump == 0) && barStart) {
     barStart = false;
     playerScript.forceJump(jumpPower);
   }
 }

and add this to the player:

 function forceJump(jumpPower : float)
 {
    if (grounded)
    {
          GetComponent<Rigidbody2D>().velocity = Vector3(0,0,0);
          GetComponent<Rigidbody2D>().AddForce (Vector2(-3,(jumpPower/7)), ForceMode2D.Impulse);
          GetComponent<Rigidbody2D>().mass = 2;
          Destroy(GameObject.FindWithTag("PowerMeter"));
    }
 }
Comment
Add comment · Show 4 · 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 ciwiaf · Apr 19, 2015 at 06:29 AM 0
Share

Though I ended up changing my whole code a bit, I use this advice of yours, so I marked this as an answer. Thanks! :)

avatar image tanoshimi · Apr 19, 2015 at 09:00 AM 0
Share

@ciwiaf - you haven't marked this as the answer... click the tick button :)

avatar image ciwiaf · Apr 19, 2015 at 10:38 AM 0
Share

Oh, I thought I already did, haha.. Done!

avatar image vargata · Apr 19, 2015 at 12:57 PM 0
Share

glad that it helped you, good luck with your project :)

avatar image
0

Answer by DiegoSLTS · Apr 18, 2015 at 12:57 PM

It might be a problem with the order of execution. Both functions are "Update" functions, and Unity will always execute one before the other. The problem is you can't (and shouldn't) force a certain order, so maybe the Update for your second script is runing before the Update from the first script.

You can try using a different update function, you also have FixedUpdate that runs before all Update functions, and LateUpdate that runs after all Update functions. Look here for more details: http://docs.unity3d.com/Manual/ExecutionOrder.html

Comment
Add comment · Show 1 · 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 ciwiaf · Apr 19, 2015 at 06:29 AM 0
Share

yea, this is what I was thinking about before, but, I've tried those 3 Update, still got the same result.

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

19 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

Related Questions

Dynamically pass integer values into separate scripts in C#? 4 Answers

how to make an animation that can take value from script ? 0 Answers

Instantiate question 1 Answer

My mouse sensitivity value don't update in other function than sliderValueChange 0 Answers

Trying to pass a float value from a prefab to another script that compares values from multiple prefabs 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