x


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??

more ▼

asked Jun 27 '11 at 05:55 PM

zachypin gravatar image

zachypin
86 36 38 38

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

1 answer: sort voted first

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.

more ▼

answered Jun 27 '11 at 06:11 PM

GuyTidhar gravatar image

GuyTidhar
2.2k 4 8 13

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.

Jun 27 '11 at 06:35 PM zachypin

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.

Jun 27 '11 at 06:49 PM dibonaj

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!

Jun 27 '11 at 07:02 PM zachypin

You're welcome. That was one of the biggest problems I had when I got into Unity.

Jun 27 '11 at 07:18 PM dibonaj
(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:

x77
x53
x36

asked: Jun 27 '11 at 05:55 PM

Seen: 1272 times

Last Updated: Jun 27 '11 at 07:18 PM