- Home /
Help accessing a variable in a C# file from a JavaScript file~
Hey everyone. I know this question has been asked before, but I followed the guide all of the other similar questions lead to; and I can't get it to work quite right. Here's a link to the original guide for reference sake. http://www.41post.com/1935/programming/unity3d-js-cs-or-cs-js-access
Currently, this is my code: var DamageObject : GameObject; var csScript : vp_PlayerDamageHandler;
function Awake()
{
//Get the CSharp Script
csScript = DamageObject.GetComponent("vp_PlayerDamageHandler");
}
function OnGUI()
{
GUI.Label(new Rect(10,30,500,20), csScript.isDead);
}
isDead is the boolean variable I want to access from the C# script vp_PlayerDamageHandler. The original code used this.Getcomponent, so figuring it would look in the object this script is on- I declared a DamageObject variable.
With the above code, I receive the following error: "The name 'vp_PlayerDamageHandler' does not denote a valid type ('not found'). "
Thanks for anyone who can help, it's greatly appreciated as always.
Answer by aldonaletto · Nov 04, 2013 at 11:45 PM
There's a last resource that can be used to communicate among scripts written in different languages and in any order: SendMessage. This method calls a function by name in the target object, thus no unknown type errors occur. Only a single argument may be passed to the function, but with some smart tricks one can send and even receive more complex data - take a look at this question for more details.
Answer by Dracorat · Nov 04, 2013 at 08:50 PM
Assuming the item is called vp_PlayerDamageHandler in the inspector, all you need to do is lose the quotes. Or to be even more accurate:
var csScript = DamageObject.GetComponent(vp_PlayerDamageHandler);
Thanks a lot for the quick response, I don't know why I didn't think to remove the quotes. Sadly however, I'm still getting an error. Just to be sure and clear any possible confusion, I'm putting in a code block with my code below.
"Assets/Custom Assets/Scripts/isDead.js(5,42): BCE0005: $$anonymous$$ identifier: 'vp_PlayerDamageHandler'."
var DamageObject : GameObject;
var csScript = DamageObject.GetComponent(vp_PlayerDamageHandler);
function Awake()
{
}
function OnGUI()
{
GUI.Label(new Rect(10,30,500,20), csScript.isDead);
}
Screenshot of the inspector for the script you're trying to access?
Here's a link to a photo of it. I tried uploading it via the forum, but it displayed the picture in-front of the 'Accept' button and wouldn't let me.. Yeah.
http://i.imgur.com/BXeSdEe.png
Please keep in-$$anonymous$$d, the JavaScript file we are working on is on a completely separate game entity. I.e: Why I created the DamageObject variable, to set the proper game object for .Getcomponent. Thanks again.
There's something else at play - I just found this. See this: http://answers.unity3d.com/questions/252865/accessing-a-c-field-from-javascript.html
Your answer

Follow this Question
Related Questions
How to assign a Variable with a Raycast 1 Answer
GetComponent, What is it ? 1 Answer
Function variable problem. 1 Answer
Trying to convert a JC script to C # 2 Answers
Where is the missing semicolon? 1 Answer