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 Gamershaze · Nov 04, 2013 at 08:41 PM · javascriptvariable

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.

Comment
Add comment
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

2 Replies

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

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.

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 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);

Comment
Add comment · Show 4 · 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 Gamershaze · Nov 04, 2013 at 09:00 PM 0
Share

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);
 }
 
avatar image Dracorat · Nov 04, 2013 at 09:30 PM 0
Share

Screenshot of the inspector for the script you're trying to access?

avatar image Gamershaze · Nov 04, 2013 at 09:39 PM 0
Share

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.

avatar image Dracorat · Nov 04, 2013 at 10:51 PM 0
Share

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

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

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


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