"a" key down to make animation play problem

I did an animation of a widget within a model in Maya, brought it into Unity(2.5_windows), and would like the widget animation to play when I hit the “a” key (“Left”).

But, I’m NOT a programmer, so I’m dealing with learning UnityScript… sigh…

My non working code is

function Update() {
			
		if (Input.GetButton ("Left")) {
				transform.root.animation.Play("openLeft");
		}
}

which, of course doesn’t work (or I wouldn’t be writing this). When I tried putting it onto the model I got an error:
(Can’t add script behaviour OpenAction. The scripts file name does not match the name of the class defined in the script!)

I would like to know what this means and I’d like to know how to code something that would do the job.

Any help/pointers would be very appreciated.

Thanks in advance.

Hermit

a)
You should do this kind of code in OnGUI, not in Update.

function OnGUI()
{
    if(Input.GetButton ("Left"))
    {
        transform.root.animation.Play("openLeft");
    } 
}

b)
The name of the class must always be the same as the name of the source file, the class is in, in Unity.

You should definitely not do that kind of code in OnGUI. Update is correct.

When using Javascript, the script can be called anything you want (although stay away from duplicating Unity classes, like don’t name scripts GUI, etc.). I’d try reimporting the script; this seems to be a bug that occasionally happens to some people.

–Eric

Kaiserludi, thanks for your quick response, however… it still gives me the same error message.

Possibly it would help if you could explain what

means. I really have no idea what the class name is or what the source file that the class is in is (unless you’re talking about the js script). :?

Sorry, I mismatched Input.GetButton with GUI.Button.
You are, of course, correct.

OK, did not know, that was different in JavaScript, because I only use C#.
So

is only correct for C#.

Thanks, Eric. Okay, changed it back. Still the error.

I assume the name “FlipperAction” isn’t a duplicate of any Unity class.

I tried reimporting (numerous times) and even closed Unity, restarted and did it again. Still giving me nothing but the same error.

There are cases where you don’t need to have a script and class names matching in C#. Library stuff that doesn’t inherit from MonoBehaviour, for example. It is always stated so absolute on the forum, that I had to pause to ask Joe what I was supposed to name a file for this:

public interface Graph<T>{ ... }

The answer, which makes total sense in hindsight, is “whatever”.

Back to the original post… are you sure it is this file creating the problem and that the problem is tied to the code you posted? I only ask because the name mentioned in the error isn’t tied to the name you just said. Or am I missing something? (I’m sleepy… cold medication has me a bit groggy today)

Bummer. Now I’m getting:

Maybe someone could explain what that’s about?

Thanks,
Hermit

Your script has an error and can’t compile. Please post the full script. Also, what errors is it giving you (that isn’t the only error)

Correct, of course, but I did not want to formulate it to complex: If a name mismatch between file and class would have been the reason for the problem in this case, it would have been no direct help to know, that it would have been ok for other classes.

Honestly, I’m not too sure of anything at the moment. :shock:

Let me see… you mean the name widget isn’t the same as “openLeft”? openLeft is the animation frame name (11 to 15) I set up on importing the model from Maya.

Thanks

Hermit

140489--5126--$animationsetup_103.jpg

Function Update() {
			
		if (Input.GetButton ("Left")) {
				transform.root.animation.Play("openLeft");
		}
}

Thanks

Hermit

What error messages beyond “Can’t add script. Cant’s add script behaviour FlipperAction. You need to fix all compile errors in all scripts first!” are you getting?

Update…

Took out another script and changed Function Update() to function Update().

No more errors. Granted, the code doesn’t do what I want and I’m getting a “UnityException:Input Button Left is not setup” in the console, but at least now…

No more errors.

Now to figure out how to set the button up.:lol:

Hermit

“Function” should be “function”