• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by ben · Mar 10, 2011 at 06:18 PM · c#javascriptgetcomponentfunction

get component javascript from a c# script

i have a c# script as my main script.. while clicking mouse i like to access a function from my another script created in java script......... is it possible if yes then how?

thanks in advance

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer Wiki

Answer by alberthci · Apr 12, 2011 at 02:17 PM

http://www.41post.com/1935/programming/unity3d-js-cs-or-cs-js-access

...The script you want to have access must be inside the "Standard Assets" or the "Plugins" folder. The other script has to be placed outside these folders. After this step, just call the GetComponent<...>() method as any other Component...

Comment
Add comment · Show 1 · 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 ben · Apr 13, 2011 at 12:59 PM 0
Share

@alberthci - thank u very much

avatar image
-1
Wiki

Answer by Steven-Walker · Mar 10, 2011 at 06:40 PM

Scripts are class objects. Javascript files automatically inherit from MonoBehaviour unless otherwise declared, with the name of the file as the class name.

So for example, if you have a script named GameController.js, you can create instances of it in your C# scripts like so:

GameController gc = new GameController();

You can then access any methods or properties just as you would with any other class.

Comment
Add comment · Show 3 · 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 ben · Mar 10, 2011 at 07:11 PM 0
Share

its not working

avatar image ben · Mar 10, 2011 at 07:13 PM 0
Share

in my scene have a button while button clicked nextScene = 1 activated

if(nextScene == 1) {

     ///////acces js pgm from here    
             
         }
avatar image ben · Mar 10, 2011 at 07:15 PM 0
Share

shows error as like follows, my .js name is ScriptName.js

error CS0246: The type or namespace name `ScriptName' could not be found. Are you missing a using directive or an assembly reference?

avatar image
0
Wiki

Answer by Parthon · Apr 04, 2011 at 08:41 PM

Are you just wanting to access the script directly, or call a function on a component. They are two different things, so careful how you word it.

I'm not sure if you can GetComponent from C# to javascript because of how type-strong C# is.

Have you tried a SendMessage? use SendMessage("functionname",argument);

Comment
Add comment · Show 2 · 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 ben · Apr 05, 2011 at 12:34 PM 0
Share

no i haven't tried send message............. ya i wanna access the whole script

avatar image Parthon · Apr 06, 2011 at 07:27 AM 0
Share

in that case you would try

ScriptName script = GetComponent();

to get the script

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Multiple Cars not working 1 Answer

Calling JS Static Function from C# Script 1 Answer

What will happen if I GetComponent in this way? (Example) 1 Answer

What is this C# code in javascript 0 Answers

What am I doing wrong? (converting a javascript code to c#) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges