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 maroltl · Mar 19, 2014 at 07:37 PM · variablesaccessing from any script

accesing variable from other script

I have been trying to access variable speed (and change it) which is in script A from script B.

i read threads on forums and on the internet, but nothing seems to work.

Comment
Add comment · Show 3
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 antk · Mar 19, 2014 at 07:43 PM 1
Share

Example from: https://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html

     function Update () {
         // To access public variables and functions 
         // in another script attached to the same game object.
         // (ScriptName is the name of the javascript file)
         var other : ScriptName = gameObject.GetComponent(ScriptName);
         // Call the function DoSomething on the script
         other.DoSomething ();
         // set another variable in the other script instance
         other.someVariable = 5;
     }
avatar image antk · Mar 19, 2014 at 07:43 PM 1
Share

also, maybe a sample of your code that doesn't work?

avatar image maroltl · Mar 20, 2014 at 05:31 PM 0
Share

sorry for late response. i would like to access variable from another script, which is not on same GameObject. The upper solution would work fine if both scripts were attached on same GameObject.

4 Replies

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

Answer by maroltl · Mar 20, 2014 at 06:13 PM

I finally got it working with

GameObject.Find("diger").GetComponent(movement).speed = 5;

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 sriram90 · Mar 20, 2014 at 06:16 PM 1
Share

You have to accept anyone's answer if its help to you :) Not yours

avatar image
1

Answer by MousePods · Mar 20, 2014 at 05:44 PM

There are multiple ways to do this.

Just go to this page: Unity Doc and hit CTRL + F and type in "Find". All of these methods will find a gameobject based on a string. Then you just use GetComponent<>()

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 sriram90 · Mar 20, 2014 at 05:48 PM

Using GetComponent() you can achieve it.

If you're using C# make your variable as Public static int (or) some other data type, and access 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 Jester0311 · Mar 20, 2014 at 06:42 PM

I despise Java but i think what your looking for is a global or public variable in which i think is declared above your start() and update() blocks this should be able to be accessed from another script you may need to use .find method when calling it.

EDIT

Found this tutorial that may help.

Unity Tutorial 6 - Accessing scripts

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

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

24 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

Related Questions

How would I change the value of one prefab's variable via script without changing all of them? 1 Answer

Error in accessing variables from another script 1 Answer

Get access and Flip between different variables from GameManager 0 Answers

Accessing variables from scripts issues 1 Answer

reference to script not working 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