x


Refrencing a js script on another GO from a cs script

I'm having trouble refrencing a javascript that is attached to my HUD GameObject from one of my C# scripts. Is there a way to do it that would help me access a funtion from the javascript on the HUD GameObject? This is what I was trying...

    GameObject hud = GameObject.FindGameObjectWithTag("HUD");

    HUDScript _hud = hud.GetComponent("HUDScript");

    _hud.AddMoney(50);
more ▼

asked Feb 17 '11 at 01:37 PM

pdh1kid gravatar image

pdh1kid
54 8 9 13

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The easiest two methods:

1) Put the js file into a folder called Plugins in the root of the project panel (i.e. Project/Assets/Plugins), then use it as if it were a c# component:

HudScript _hud = hud.GetComponent<HUDScript>();

2) Use SendMessage instead of going through GetComponent and calling the function. This is going to be slower however:

hud.SendMessage("AddMoney", 50);
more ▼

answered Feb 17 '11 at 02:55 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3460
x404

asked: Feb 17 '11 at 01:37 PM

Seen: 1128 times

Last Updated: Feb 17 '11 at 01:37 PM