GetComponent from JS in C#

Hallo there,

I am trying to access a JS script with C#, but it doesn’t seem to work that well…

I have a empty game object with my C# and JS scripts on it and try to access the JS with this:

handler h = this.GetComponent<handler>();

handler being the JS file…

But all I receive is:

I already looked around, but couldn’t really find any answers… :s I read something about having to place the JS in some special folder that gets compiled before the C# file, but I kinda lost track of that page… and I’m not even sure if the line above actually would work if I would place the JS in this special folder…

So could someone please help me with this? :slight_smile:

Im pretty sure its impossible to do,
I can only suggest you to write your codes only in C# or JS since it will give you alot of long - term problems.
if its short code,can you just rewrite it in C#?

Not necessary.

As for mixing JS and C#, you should find your answer very easily. Why no luck after a Google or forum search?

A quick google search turned this up second from the top: http://answers.unity3d.com/questions/18642/accessing-javascripts-from-c-scripts.html

I should also mention that with Unity 3.4, you can manually alter script compilation order, so you don’t even have to muck around with folders if you don’t want to.

I have the same problem here:
http://forum.unity3d.com/threads/101210-Javascript-functions-from-C-Sharp

Even if you use the Execution order and I put my js files first the CS files will still gives me:
CODE:

public fixedupdate fixedupdate;

ERROR:

error CS0118: `controlstouch.fixedupdate' is a `field' but a `type' was expected

This is with my fixedupdate.js file being set to execute first before controlstouch.cs.

I’m trying to think of a work around for this if anyone can help me out. The only thing I can think of is have the JS script check on every update if the object has my CS script attached and if its not then the countdown finished on my CS script and the I’ve destroyed the component.

What do you guys think?

the error there has nothing to do compilation order. Also execution order will not impact compilation at all, the compilation happens in “parallel” for the same compilation step.

the problem you have there is more that you declare something incorrectly, please provide the fully code unitynewb cause I’m pretty sure that you are doing something thats unrelated to that part but the stuff around it

The problem with your code is that you have a variable with the same name as a class, which is going to give you an error. :wink:

Ok Here are the 2 scripts. Copy the text into the same filename and put them in a new blank projects Plugins folder. I even tried setting the execution order and putting the js file first then the cs file and I get the same error.

Assets/Plugins/controlstouch.cs(8,16): error CS0246: The type or namespace name `fixedupdate' could not be found. Are you missing a using directive or an assembly reference?

fixedupdate.js

import System.Collections.Generic;

static var gameobjects = new Dictionary.<String,GameObject>();
static var audioobjects = new Dictionary.<String,AudioSource>();
static var transforms = new Dictionary.<String,Transform>();
static var hingejoints = new Dictionary.<String,HingeJoint>();
static var joints = new Dictionary.<String,ConfigurableJoint>();
static var rigidbodies = new Dictionary.<String,Rigidbody>();
function Update () {
}

controlstouch.cs

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class controlstouch : MonoBehaviour {
	
	
	public fixedupdate tmpinfo;
	// Use this for initialization
	void Start () {
		GameObject tmpObject = (GameObject)GameObject.Find("c1");
		fixedupdate tmpinfo = tmpObject.GetComponent(typeof(fixedupdate)) as fixedupdate;
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

Why do you have a class named fixedupdate? It’s bad practice to have a class with the same name as a function…

Besides that, you’re tmpinfo as an instance variable and then redefining it locally, which once again, will cause problems.

Ok well lets do this then:

Same error:

Assets/Plugins/controlstouch.cs(7,16): error CS0246: The type or namespace name `tempjs' could not be found. Are you missing a using directive or an assembly reference?

tempjs.js

import System.Collections.Generic;

static var gameobjects = new Dictionary.<String,GameObject>();
static var audioobjects = new Dictionary.<String,AudioSource>();
static var transforms = new Dictionary.<String,Transform>();
static var hingejoints = new Dictionary.<String,HingeJoint>();
static var joints = new Dictionary.<String,ConfigurableJoint>();
static var rigidbodies = new Dictionary.<String,Rigidbody>();

controlstouch.js

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class controlstouch : MonoBehaviour {
	
	public tempjs tmpinfo;
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

I noticed the errors before in my “void Start”, but I was focusing on the actual error regarding this topic. Please try putting this code in a fresh project and let me know if you receive the same error I do.

If I’m doing something wrong or if there is a better way to do this please let me know I’m a beginner and will make mistakes often, but I learn from them.

This may sound silly but - do you actually have a class declaration in tempjs.js or just a list of variables as you pasted above?

You’re also trying to declare a member of type tempjs but all you have are static variables. Is this intentional? Is there any reason you can’t simply reference them directly?

Just a list of variables that I update from other scripts. There are global variables that I use in the game. I know a class would be better, but I started this script way back and this was what I found on the forum on how to create Global variables and never changed them to their own class.

How would I be able to reference them directly and update?

I pasted your code into a couple of scripts, placed tempjs in Standard Assets, and it compiles without error.
http://cl.ly/0n1a2Q2s2v3p452y3u2u

Oh maybe thats my problem, I was putting them both in my Plugins folder… Sorry about that.

Thanks for helping me figure this out. You saved me hours and hours of rewriting js code. :slight_smile:

This is starting to get on my nerves… :expressionless:

I tried…

handler h = (handler) gameObject.GetComponent<handler>();

and

handler h = (handler) gameObject.GetComponent("handler");

but neither of them work as soon as I call them… it just gives me a pointless error message. (placed the handler.js in the Plugins folder)

I also tried… GameObject gO = GameObject.Find("GameObject");
And it gives me another error that doesn’t have any description at all…

Any other ides? What am I actually looking for with the find function? A tag attached to a GO or the name of the GO? I tried the standard name and changing it but it doesn’t work… or can I only find other GO’s but not the GO itself that the script is attached to? :confused:

GameObject.Find(“GameObject”) will look for a gameObject named “GameObject”.

What is the error you get with your first line of code?

handler h = (handler) gameObject.GetComponent<handler>();

UnityEngine.Component:get_gameObject()
connection:HandleClientComm(Object) (at Assets\Scripts\connection.cs:85)

handler h = (handler) gameObject.GetComponent("handler");

UnityEngine.Component:get_gameObject()
connection:HandleClientComm(Object) (at Assets\Scripts\connection.cs:86)

GameObject  gO = GameObject.Find("GameObject");

UnityEngine.GameObject:Find(String)
connection:HandleClientComm(Object) (at Assets\Scripts\connection.cs:92)

:confused: