GameObject.GetComponent.<Animation>().Play("DoorOpen")

Hello everybody, Im starting with unity, and this is my first thread in this forum. Congrats for the quality of the forum. I am very motivated with this program, and Im watching a lot of tutorials everywhere, but im stuck with something and I dont find anywhere where the mistake is.

Im doing the animation of a door opening and closing, with a raycast. There are a lot of tutorials about it, but all of them are using different syntax in the function to activate an animation, as all of them are programed for older versions, and it has changed for Unity 5. That´s why Im not sure if it is a syntax problem or something with the animation, and, in that case, what could be wrong…

I add the code from the script atached to the first person controller. You can check just what in Bold letters is. everything else is working fine. I also attach the pictures to show how the animation is added to the gameobject, the script to the First person controller, and the animation file.

Sorry for the long text but I wanted to explain as clear as possible. Thanks in advanced. I have been a whole day looking for the mistake and I dont understand whats wrong…

#pragma strict
private var guiShow : boolean = false;
var isOpen : boolean = false;
var door : GameObject;
var rayLength = 10;
function Update()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLength))

{
if(hit.collider.gameObject.tag == “Door”)
{
guiShow = true;
if(Input.GetKeyDown(“e”) && isOpen == false)
{
door.GetComponent.().Play(“DoorOpen”); //Here it doesnt activate the animation
isOpen = true; //isOpen is activated
guiShow = false; //guiShow is deactivated
}

else if(Input.GetKeyDown(“e”) && isOpen == true)
{
door.GetComponent.().Play(“DoorClose”);
isOpen = false;
guiShow = false;
}
}
}

else
{
guiShow = false;
}

print ("IsOpen = “+isOpen+” ; fwd = “+fwd+” ; hit = “+hit+” ; TransPosition = “+transform.position+” ; hit.collider.gameObject.tag = "+hit.collider.gameObject.tag);
}
function OnGUI()
{
if(guiShow == true && isOpen == false)
{
GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), “Use Door”);
}
}

print ("IsOpen = “+isOpen+” ; fwd = “+fwd+” ; hit = “+hit+” ; TransPosition = “+transform.position+” ; hit.collider.gameObject.tag = "+hit.collider.gameObject.tag);
}
function OnGUI()
{
if(guiShow == true && isOpen == false)
{
GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 100, 25), “Use Door”);
}
}



Is that . your problem?

Please use code tags when sharing code, as it makes it much easier to read and figure out where you may be going wrong.

My favorite way of debugging code where things aren’t happening that I expect to be is adding a console log at various points in the script. That way I can see which things are happening, and which things aren’t. Example:

if(Physics.Raycast(transform.position, fwd, hit, rayLength)) {
    if(hit.collider.gameObject.tag == "Door") {
        Debug.Log("[Raycast hit object tagged 'Door']");
        guiShow = true;
      
        if(Input.GetKeyDown("e") && isOpen == false) {
            Debug.Log("[['e' key pressed]]");
            // Other code here
        }
    }
}

If you’re not getting error messages, this is a good way to dial in on where the problem is happening.

Edit: As @Cromfell pointed out, that is definitely a rogue period there, too.

It’s not a rogue period. UnityScript uses a period before generic arguments.

I’d probably refactor the code to make it a little easier to debug

if (Input.GetKeyDown())
{
    if (isOpen)
    {
        Play("DoorClose");
    }
    else
    {
        Play("DoorClose");
    }
    isOpen = !isOpen;
}

“Animation” is probably the wrong class. Animation is a legacy class; there’s a new class, Animator, that functions differently. If you don’t select the “Legacy” option when you import the animation, then it will import with an Animator component, not Animation.

If this is the problem, though, it should error out on that line - it’d give you a NullReferenceException when you try to use the Animation component that doesn’t exist. Do you have anything in your Console?

Except if you look at the Inspector screenshot, there is an Animation component on the door…

Oh, I didn’t even notice there were screenshots…

But this can’t be right, can it?

door.GetComponent.<Animation>()

The period in between the function name and the generic type?

Yes - that’s how UnityScript does it.

Ew, haha.

Indeed.

In either case - the logic seems to largely be in the wrong order. I would only raycast when the user presses E and then check isOpen to determine what animation to play and then just invert isOpen.

1 Like

Wow thank you for all the quick answers, as @StarManta has said, I get this error at the console ( I add a screenshot too):

"The AnimationClip ‘DoorOpen’ used by the Animation component ‘door’ must be marked as Legacy.
UnityEngine.Animation:Play(String)
DoorOpening:Update() (at Assets/DoorOpening.js:32)"

I dont understand well the concept of the legacy, how should I fix the error then?

Either switch to using an Animator component or change the import settings for your animation to Legacy.

1 Like

Thank you @KelsoMRK for the idea of changing the order, it is another option. But in this case, I need to do it this way, so it appears a message in the moment you are in front of the door

Thank you KelsoMRK, I will check that out, Now that I know where the problem is I should be able to fix it. I must go on holyday now. I will answer on monday if it is working.

Thank you everybody for the help!! Cheers!

Allahu Akbar!

1 Like

don’t know if anyone else already said it but the following worked for me:

GetComponent().Play(“animation Name”);

use exactly like that

1 Like

I guess they figured it out after 3 and a half years… :roll_eyes:

2 Likes