Jeffery Howard
Jeffery Howard

Reputation: 1

unity variable access from one script to another

So I have have a script for a text mesh on a 3D text in unity and I want it to access a variable from a cube that has an OnMouseDown function, inside that function it changes the score and i want that score to be outputted into the text. the cube script is wrote in javascript while I wrote the text script in cs. Anyone have any ideas?

Upvotes: 0

Views: 151

Answers (1)

BjarkeCK
BjarkeCK

Reputation: 5729

Correct me if i have misunderstood your problem, i might have.

Option 1:

C# Code is compiled before js code, which means you dont have access to any classes / types written in javascript from c# code. However scripts located in a folder called Plugins is compiled before the "game code", so you could move js script in to a folder called Plugins and then have access to its types from C# code.

This is a kind of hacky approach (if it's not a plugin it should not be located in a folder called Plugins) but it would work.

Option 2:

Access whatever you need from the js file via reflection during runtime. I won't reccomend this though, because reflection is usually slow. Tricks can be done to speed things up thoguh.

Only choose this option if you absolutely insist that the javascript file should not be convertet to c#, or located in a Plugins folder.

Option 3:

Convert the javascript file to c#. This is what i would recommend.

Upvotes: 1

Related Questions