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…
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#?
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’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.
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
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 () {
}
}
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?
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?