Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jun 02, 2017 at 06:21 AM by ZachAttack9280 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by ZachAttack9280 · May 31, 2017 at 06:01 AM · c#variableaddaddingtwo

Adding two variables from two different scripts.

Hello, I am trying to figure out how to add two variables from two scripts.

So let's say we have two scripts.

ScriptOne:

  public ScriptTwo scriptTwo;
  public float mainNumber;
  public Button click;
  
  public void Click()
  {
  mainNumber += 1;
  }
 

 

So everytime to you click the button, it adds 1 to the "mainNumber" variable.

ScriptTwo:

  public float numberTwo;
  public Button click;
  
  public void Click()
  {
  numberTwo += 1;
  }

So now Button Two will do the same thing but add 1 per click to the "numberTwo" variable.

So back to ScriptOne, I want numberTwo to be added to mainNumber. Let's say numberTwo is 3 and mainNumber is 3. The ending result would be 5.

I tried doing mainNumber = mainNumber + scriptTwo.numberTwo but the number would just go up really fast and constantly. Anyone have a solution to this? (Hoping this was as clear as possible).

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 ShadyProductions · May 31, 2017 at 11:24 AM 0
Share

What is your Click method, where do you call it?

4 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by FM-Productions · Jun 01, 2017 at 08:12 AM

Quick solution, may not be the best:

Define a static class with two static variables. Those variables can then be used in any script of yours. The variables are bound to the class itself, rather than to a class instance.

 public static class StaticNumbers
 {
      public static float mainNumber;
      public static float numberTwo;
 
 }


Then simply access them in your script like:

 public void Click()
   {
   StaticNumbers.numberTwo += 1;
   }


Finally, you can simply add them like this:

 StaticNumbers.numberTwo + StaticNumbers.mainNumber

If you reset the scene/game, also remember to reset the static variables to their original value.

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 rallyall · Feb 28, 2020 at 05:28 AM 0
Share

What do you mean

Finally, you can simply add them like this:

StaticNumbers.numberTwo + StaticNumbers.mainNumber

So

public static float StaticNumbers.numberTwo + StaticNumbers.mainNumber;

Would these need to be inside a method or function? Can you do something like:

public static float StaticNumbers.numberTwo + StaticNumbers.mainNumber = finalNumber;

Then later...

public void BlahBlah(){ Debug.Log(finalNumber); }

And that all should work?

avatar image
0

Answer by ShadyProductions · May 31, 2017 at 06:59 AM

If you do a little searching on google or unity answers you will find multiple of these same questions already answered.

You have to get the script component of the gameobject if it is a monobehaviour like so:

 var script = yourobject.GetComponent<YOURSCRIPTNAME>();
 script.mainNumber = 5;

However if the script is not monobehaviour you will either have to make the script static so you can access it right away like:

ScriptName.mainNumber = 5;

or make a new instance if static is not an option like:

 var script = new YOURSCRIPTNAME();
 script.mainNumber;
Comment
Add comment · Show 3 · 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 game4444 · May 31, 2017 at 11:15 AM 1
Share

He is not asking about assigning number. He is asking about adding two numbers from 2 different scripts. Your answer is about assignment of variables from other scripts.

avatar image ShadyProductions game4444 · May 31, 2017 at 11:24 AM 0
Share

it is the same principe.

avatar image ZachAttack9280 · Jun 01, 2017 at 04:52 AM 0
Share

This is in js not c# so I don't know how to compare.

avatar image
0

Answer by AdhikS · Jun 03, 2017 at 11:33 AM

declare your variables as static.then you can change or access the value of one variable from another script

Comment
Add comment · 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
0

Answer by game4444 · Jun 03, 2017 at 11:33 AM

the number would just go up really fast?? Its not clear. Please elaborate more. If your add coding is in Update it will run again n again. It will increase your count very fast. So please explain bit more.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

324 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 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 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 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 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

C# adding ints 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

(C#) For loop variable value not changing 0 Answers

Variable References in C# from JS 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