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 Fadi · Jun 28, 2013 at 07:47 PM ·

read script from onther one ?

Hi ...

is there any way to read the value from other script; this is the idea .

example :

 Script_1.js
 var Age = 25;
 
 Script_2.js
 var OtherScript : MonoScript;
 function Start()
 {
     OtherScript = GetComponent("Script_1"); // I need the script's name to be string
     print( OtherScript.Age );
 }

Note : both Script_1.js & Script_2.js connected the the same object .

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 iwaldrop · Jun 28, 2013 at 08:38 PM 1
Share

Your 'idea' is correct. Did you try it?

This question is probably asked once a week. Did you do any googling before you asked it again? And who approved this question without simply referring the asker to a dozen other instances of the same question?

avatar image Fadi · Jun 28, 2013 at 09:46 PM 0
Share
  • I tried it , but this message I get

'Age' is not a member of 'UnityEngine.$$anonymous$$onoBehaviour'.

  • I searched a lot :(

2 Replies

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

Answer by Immanuel-Scholz · Jun 28, 2013 at 09:33 PM

As Iwaldrop said, your code should work just fine, after your corrected the typos (e.g. its MonoBehaviour. Not MonoScript).

You can get any other component of your same GameObject by just passing the string-name of the other class to GetComponent().

Comment
Add comment · Show 12 · 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 Fadi · Jun 28, 2013 at 09:44 PM 0
Share

thank u ....

this the message I get :

'Age' is not a member of 'UnityEngine.$$anonymous$$onoBehaviour'.

avatar image iwaldrop · Jun 28, 2013 at 09:45 PM 0
Share

OtherScript isn't a monobehaviour, actually. It's a Script_1.

avatar image Immanuel-Scholz · Jun 28, 2013 at 09:52 PM 0
Share

Oh, right. If you use the version of GetComponent where you pass the string, you will get a basic "$$anonymous$$onoBehaviour" back (the base class of all scripts). You have to tell the compiler what exact type it is. (It can't know, since you passed the name of the type ;)).

You can use this:

 var OtherScript : Script_1 = GetComponent("Script_1") as Script_1
 print (OtherScript.Age)


But then, of course, you are stating the Script_1 directly as type ins$$anonymous$$d of only its name.

You could also just write

 var OtherScript = GetComponent.<Script_1>()
avatar image Immanuel-Scholz · Jun 28, 2013 at 10:16 PM 1
Share
  • don't want to write the name of the script "var OtherScript : Script_1 " I want to access from other script*

Hm... Why don't you want to write the type name of the other component? Are you thinking this is some kind of bad coding style?

Remember that "Script_1" is not only the "name" of your script, but actually the type of the component you are accessing. If there only happens to be one single instance of that compoenent, that's doesn't make it "bad" to access the script by its type.

I'd say, if you write GetComponent("Script_1") that is worse in terms of "hardcoded and tightly coupled coding style" because you now use the type of the other component (Script_1) by its textual representation (its name "Script_1"). $$anonymous$$g. if you would change the type name later (rename the script and class), then the code would break at runtime without compile errors.

avatar image Immanuel-Scholz · Jun 28, 2013 at 10:31 PM 1
Share

GetComponent work with inheritance. You may want to create a base class containing all common variables (like Age) and either use GetComponent.< Script_Base >() to gain "any first component with that base class" unity finds or use GetComponents.< Script_Base >() (notice the s) to get them all in an array.

Show more comments
avatar image
0

Answer by iwaldrop · Jun 28, 2013 at 10:16 PM

Script_1.js:

 var age = 25;

Script_2.js:

 var otherScript : Script_1;
 function Start()
 {
     otherScript = GetComponent.< Script_1>();
     print(otherScript.age);
 }

Link to Unity Docs, 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

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to add a cooldown to a move. 1 Answer

Objectives based on object appear. 2 Answers

how to combine 2 conflicing scripts 1 Answer

My PC can't open any sort of script 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