- Home /
Call CS from JS in one gameobject
I've looked through a bunch of the questions on here involving this but they don't answer in a way that seems logical for my use. So here it goes: (I've added in the script because I find it useful answering questions with it, though I don't think it may be necessary to answer this question)
Thanks for the look!
First the Hierarchy.
-Animations
o-Communicator
oo-Interaction
On the "Interaction" level I have a child object that I click/interact with and it triggers CS code called "UpButton". This code triggers a boolean value to be made true in a function "WasUpPressed":
bool upPressed = false;
void OnMouseDown()
{
upPressed = true;
}
bool WasUpPressed()
{
return upPressed;
}
Now on the "Communicator" level I have a JS. This tells an animation to play on the "Animation" level if it "hears" that "WasUpPressed" returns true :
var upbutton = false;
function Update ()
{
upbutton = GetComponent(UpButton);
upbutton.WasUpPressed();
if(upbutton)
{
transform.parent.gameObject.animation.Play("m-1");
}
}
To me it seems as if this would work, however, I can't even see if it does because it won't compile in the Project for me to be able to even attach it to my gameObject. The error I get is "Unknown Identifier: 'UpButton'" which will obviously be the case if the script isn't attached to the gameObject. So how do I fix this??
Answer by GuyTidhar · Jun 27, 2011 at 06:11 PM
The CS script is not compiled before the JS.
If you place the JS in a folder other then "Plugins" folder, and make sure the CS is under the "Plugins" folder, the code in the CS will be known in the JS.
That seems like such a work around though. I have all my folders setup so that I know where all my code is and it's nice and orderly. You're saying for a JS to talk to a CS I have to make Unity think that my CS is a plugin? No offense to you but that just seems absurd if that's the only method Unity deals with this.
http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html
Unity compiles things in a certain order. It is not a work around, it is just the way it works.
It's just weird I can't just attach a flag or something that tells Unity to look at this script before this one. Pretty much a user guided dependency hierarchy.
But anyways!
I put the scripts inside of Plugins as you guided and one error went away. Not sure if it's directly related so I created a new question.
http://answers.unity3d.com/questions/136247/component-call-not-recognizing-boolean-syntax-erro.html Thanks for the info guys!
You're welcome. That was one of the biggest problems I had when I got into Unity.