- Home /
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 .
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?
I tried it , but this message I get
'Age' is not a member of 'UnityEngine.$$anonymous$$onoBehaviour'.
I searched a lot :(
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().
thank u ....
this the message I get :
'Age' is not a member of 'UnityEngine.$$anonymous$$onoBehaviour'.
OtherScript isn't a monobehaviour, actually. It's a Script_1.
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>()
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.
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.
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);
}
Your answer
Follow this Question
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