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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by TheCount · Dec 28, 2013 at 05:31 PM · javascriptvariablechangeaccess

Access and Change variables from other Scripts?

Hey.

I know there are a lot of ways out there to access variables from other scripts.

A few that I know include:

static variables;

calling anotherScript;

but neither of these work for what I want to do. I have looked online in many places and haven't found exactly what I'm looking for, and I'm sure I'm not the only one.

What needs to happen is all of these ways to access variables from other scripts should all be in one place, so if you know any good ways, leave them as an answer to this post with an example and explanation and we'll try to make this post become gold for all newbie programmers.

Please leave the example in the following format so that it is easy to sift through all of the answers.

Example Format:

*Insert Explanation of what we need to do

*Insert code example on how to do it

*Variable names to be used are: 'Script1', the name of the script which contains the variable 'string1', and 'Script2', the name of the script which contains the variable 'string2'.

Remember the goal of the answer should be to A. Show how 'Script1' can change it's 'string1' to the value of the variable in 'Script2', 'string1'. And/Or B. Show how 'Script1' can change the value of the variable 'string2' in 'Script2', to the value of 'string1' in 'Script1'.

Make sure to do the answer in javascript. If you can also show code in C# than do both. UpVote the best answers!

Comment
Add comment · Show 2
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 KellyThomas · Dec 28, 2013 at 05:39 PM 0
Share

"...but neither of these work for what I want to do."

What do you want to do in your current project that your existing techniques don't solve?

avatar image TheCount · Dec 28, 2013 at 05:43 PM 0
Share

Really I just think we should consolidate all the techniques to one post. But what I want to do is change a variable of an instantiated object from another instantiated object that gets the variable from another object's script. If that makes any sense XD

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by emc233 · Dec 28, 2013 at 05:40 PM

I will give you a few lines of my own code to help you. I am not sure what you mean by "access variables from other scripts should all be in one place"

GameObject.Find("Plane").GetComponent(forcemat).boo

Plane is the name of the gameobject that I am taking the script, forcemat from. boo is the boolean value inside of the forcemat script which is attached to the gameobject named Plane.

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
1

Answer by thaiscorpion · Dec 28, 2013 at 05:44 PM

Another way to do this is creating a static class that contains the variables you want to access:

 static class GlobalVars {
 
     var string1 : String;
 
 }

Then on anyother script you can access this string by simply putting:

 GlobalVars.string1

Please note that the static class script doesn't need to be attached to any object.

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 TheCount · Dec 28, 2013 at 05:50 PM 0
Share

Thanks for formatting it well!

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

21 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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Change all value of variable 1 Answer

Gun Script Help 2 Answers

Having both mouse orbit and smooth follow work at the same time? 0 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