- Home /
SOLVED - Accessing a C# script value from another GameObject's JavaScript
I read a lot of forum threads and answers about accessing other GameObject's scripts. In case of C# I am totally fine, but I need to write a reference into a JavaScript to access a C# script. As long as they are on the same GameObject, everything is fine. But how do you modify the following line (not being attached to "Rmenu" object) to access "Rmenu" object, "Page" script "PageNumber" public variable?
var x = GameObject.Find("Rmenu").GetComponent(Page).PageNumber;
I'm keep getting the error message: BCE0018: The name 'Page' does not denote a valid type ('not found').
As always any answer and hints are welcome and thanked in advance.
Please read this: http://answers.unity3d.com/questions/540026/how-to-move-data-between-js-cs-script-.html
Accepted answer will give you a quick info, while reading links from my answer will help you understand why it works this way.
Please accept/upvote helpful answers and comments to your previous questions (you can even mark your own answer if it is valid). To learn why it is important, I suggest reading FAQ and watching tutorial video (links on the right).
You'll have to deal with this kind of errors whether you program in C# or JS, as long as you need to access scripts written in a different language. The best option is to just stick to one language.
And I disagree that C# is less flexible :)
The problem is that if you buy something from the asset store, it depends on the programmer's favor our whether he/she uses Javascript or C#.
I am a noob in program$$anonymous$$g, therefore I am struggling with the access of all public variables no matter if C# or Javascript.
The best would be something like Visual Basic, making a variable public would be seen all over the place. :)
I agree that asset store products use different languages, but majority of their scripts land in Standard Assets folder, so it is possible to reference them in any language.
Answer by srancsik · Oct 22, 2013 at 12:04 PM
Dear ArkaneX,
I can not believe I fell into the same trap as previously. Due to a similar un-accessibility I started putting my scripts into "Standard Assets" folder. As you suggested I moved my Javascript out of this folder and immediately everything was working. Reading the referenced links you sent me, I understand that in C# this is not a problem. I highly consider sticking to C# only, in spite it seems less flexible when programming. But anyway:
I AM VERY GREATFUL FOR YOUR HELP, THANK YOU!!!
Answer by btb200 · Oct 22, 2013 at 12:28 PM
You have to write public method in your JS Class:
function SetX(xVal){
}
and in your C# class use:
GameObject.Find("GameObjectName").GetComponent ("JSClassName").SendMessage ("SetX", xValue);
It will send value of X from C# class to JS class.
Your answer
Follow this Question
Related Questions
Getting a variable from another object 1 Answer
How to access gameObject variable script 2 Answers
Can't access variable from another script. 1 Answer
Cannot access a variable in another Script. 2 Answers
Access guiText from other object. 3 Answers