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 instruct9r · Nov 08, 2013 at 06:12 PM · valuepassmany

Pass value from several objects into one JS

Hello.

I am struggling with something here and i got little bit confused.

I want to pass (continously) a value from several different objects into one. Let's say, that i'm making side scrolling game and i have 2 buttons for running left and right. My player have a variable "inputHorizontal", which depending of it's value (1 or -1) will make the player run left / right.

Now i have to pass 1 from one of the buttons and -1 from the other into that same variable "inputHorizontal" and in the same time if i press one button and release it, inputHorizontal, shoud go back to 0. For some reason only one of the buttons work and pass value to the variable and only if i deactivate it, the other one starts working.

For the purpose i've made simple scripts, so i don't need to copy tons of code here. Here's the 2 buttons scripts, which are identical except the value passes 1 or -1.

Script name : MoveButtonLeft_JS

 #pragma strict
 
 // Public Variables
 public var inputHorizontal    : float;
 
 // Private Variables
 private var printValue        : z_PrintValue;
 private var onPress            : boolean;
 
 function Awake()
 {
     printValue = GameObject.FindGameObjectWithTag("GameController").GetComponent(z_PrintValue);
 }
 
 function Update()
 {
     printValue.inputHorizontal = inputHorizontal;
     if (onPress)
         inputHorizontal = 1;
     else
         inputHorizontal = 0;
 }
 
 function OnMouseDown()
 {
     onPress = true;
 }
 
 function OnMouseUp()
 {
     onPress = false;
 }

Here's the second button script Script Name: MoveButtonRight_JS

 #pragma strict
     
     // Public Variables
     public var inputHorizontal    : float;
     
     // Private Variables
     private var printValue        : z_PrintValue;
     private var onPress            : boolean;
     
     function Awake()
     {
         printValue = GameObject.FindGameObjectWithTag("GameController").GetComponent(z_PrintValue);
     }
     
     function Update()
     {
         printValue.inputHorizontal = inputHorizontal;
         if (onPress)
             inputHorizontal = -1;
         else
             inputHorizontal = 0;
     }
     
     function OnMouseDown()
     {
         onPress = true;
     }
     
     function OnMouseUp()
     {
         onPress = false;
     }

and this is the script, that, the above 2 buttons shoud feed the value to. This script is attached to game object, that is tagget as "GameController": Script Name: z_PrintValue

 public var inputHorizontal    : float;
 
 
 
 function Update()
 {
     print (inputHorizontal);
 }



Why only one o of the scripts is passing information and the other one doesn't :\

thanks :)

inputHorizontal

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Starwalker · Nov 08, 2013 at 08:26 PM

 function Awake()
     {
         printValue = GameObject.FindGameObjectWithTag("GameController").GetComponent(z_PrintValue);
     }


You are referencing the same script in both scripts, make the name as "z_PrintValueLeft" and "z_PrintValueRight" and add this to the same object. At a given time, if there are multiple script clones on the same object, only 1 is returned, your other script is never part of the return hence it wont work.

In Theory: Using exact same variable (field) on a common point of return will need to be seperated by some event which happens sequentially. A variable referenced in an Update function will get updated which ever calls it 1st, and then stay untill another event updates it. Both will never happen simultaneously effecting the same variable. Simultaneous input is possible, but not on the same variable name.

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 instruct9r · Nov 08, 2013 at 10:22 PM 0
Share

thanks, but i have to (somehow) pass from the 2 buttons, into that variable.

The thing is that my Player have a variable "inputHorizontal". If that variable is 1 the player runs right, if it's -1, the player run's left. These 2 buttons will make him run left or right. I can't add 2 times the script of for the Player Controller...

avatar image Starwalker · Nov 08, 2013 at 11:00 PM 0
Share

inputValue = inputHorizontal;, inputValue should be applied to Player then, this makes inputHorizontal independent and will always have a value applied.

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

17 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

Related Questions

Instantiate question 1 Answer

issues passing a value form one script to another 2 Answers

Take only one action ~ if(A = var[i]) reveals a true 1 Answer

passing a javascript array into a function sorts it? 1 Answer

Pass a function(float) as variable 2 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