Unexpected symbol "("

So I am trying to access an Animation in my script, wait for a moment, then have the other animation play. It seems to work fine, but I am it is saying I have unexpected symbols- “Unexpected symbol )” And then it has three other errors with that message. This is my code below- any help would be appreciated! I am also coding in C#.

void OpenTheDoor (){
TheDoor.GetComponent<“Animator”>().enabled=true;
yield return new WaitForSeconds(1);
TheDoor.GetComponent<“Animator”>().enabled=false;
yield return new WaitForSeconds(5);
TheDoor.GetComponent<“Animator”>().enabled=true;
yield return new WaitForSeconds(1);
TheDoor.GetComponent<“Animator”>().enabled=false;
}
}

It should mark your error. What line is it pointing to? Post the rest of the script as it seems you have a symbol that doesn’t belong somewhere.

And use code tags. Using code tags properly - Unity Engine - Unity Discussions

You do have an extra closing bracket at the end. So that could be the issue, unless that was copied as the closing bracket for the class, which is why I asked for the rest of the script.

Please use this to properly show your code on the forums Using code tags properly - Unity Engine - Unity Discussions

Also, it would seem your last “}” is what would cause your problem.

You had an extra closing curley:

// You had one too many closing curley brackets
// Improvement: Cache the component so you dont overuse GetComponent
// Improvement: Cache WaitForSeconds to reduce garbage generation

void OpenTheDoor ()
{
    Animator anim = TheDoor.GetComponent<Animator>();
    WaitForSeconds shortWait = new WaitForSeconds(1f);

    anim.enabled = true;

    yield return shortWait;
    anim.enabled = false;

    yield return new WaitForSeconds(5);
    anim.enabled = true;

    yield return shortWait;
    anim.enabled = false;
}

Just to add:
The first error is usually the most important, the others can just be a side effect of the first.

EDIT:
You also tried passing a string “Animator” into GetComponent. That is not how it works. its just Animator without quotes.

I used your code and it worked great! Thank you so much.

1 Like

That was what was closing the class, but I am appreciative of the code tags you linked me to. I didn’t know those were a thing, so thanks for pointing it out!

Yeah, that was the closing bracket. I am still having issues though, so I’ll post the full script- thank you for the link to the code tags!

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class OpenDoor001 : MonoBehaviour {


public GameObject TextDisplay;
public float TheDistance = PlayerRaycast.DistanceFromTarget;
public GameObject TheDoor;



void  Update (){
    TheDistance = PlayerRaycast.DistanceFromTarget;
    if (Input.GetButtonDown("Action")) {
        if (TheDistance <= 2) {
            OpenTheDoor();

        }
    }
      
}

void  OnMouseOver (){
    if (TheDistance <= 2) {
        TextDisplay.GetComponent<Text>().text = "Press Button";
    }
}

void  OnMouseExit (){
    TextDisplay.GetComponent<Text>().text = "";
}

IEnumerator OpenTheDoor ()
{
    Animator anim = TheDoor.GetComponent<Animator>();
    WaitForSeconds shortWait = new WaitForSeconds(1f);
    anim.enabled = true;
    yield return shortWait;
    anim.enabled = false;
    yield return new WaitForSeconds(5);
    anim.enabled = true;
    yield return shortWait;
    anim.enabled = false;
}
}

So basically this still isn’t working. I would go over to the button, hit e, and nothing will happen. I have the “action” button set to positive button e, but It won’t work. I’m really stumped on this- again, any help is appreciated beyond words!

I used your code and it doesn’t bring any errors, so thank you for that! But if it’s not too much trouble, could you take a look at the script I just posted? The key I am using to press to open the door is not working, and I am truly stumped by it.

You need to figure out where the problem is. Put debug.log lines before and after line 17 to determine if the button press is both being registered and if the value of TheDistance is not what you were expecting. You could also temporarily swap out your Input.GetButtonDown line with Input.GetKeyDown(“e”) to see if the issue is with how you’ve configured the input manager.

When you run into these kinds of issues you should just always start with a liberal use of debug.log

TheDistance = PlayerRaycast.DistanceFromTarget; Make sure this is recalculating distance.

But as @Joe-Censored mentioned. Add a debug to make sure parts of the code are running. Debug values to make sure you are getting the values you expect to.

Debug.Log(TheDistance); for example will help you see if you are getting the expected value.

Thank you for the tip- so I decided to use a debug that says “Hey” When you press the right key to activate the door. The log came up just fine, and continued to do so. The animation just won’t play. I’m not sure what else to do here. The Animation just isn’t being triggered by the button.

I used the Debug.Log, and it works fine- It comes up whenever I hit the correct button while looking at the switch. But the Animation just won’t play when I hit the right button.

Should the door be opening by animation?
If so why are you just enabling and disabling the animator? What do you think that should do?
You should be settings up a parameter in the animator and sending it a trigger.
This shows a clear lack of research if so:
https://unity3d.com/learn/tutorials/topics/animation/animator-scripting?playlist=17099

You put the debug within the if(TheDistance <= 2)?

How is your animator setup? I don’t see you playing anything in the coroutine. @TaleOf4Gamers_1 beat me to questioning about the animator.

I did put the debug in, yeah. It worked fine. And how do you mean? I’m really new to unity and scripting, and this was a transfer from a javascript. I thought it would have worked where I wrote "
Animator anim = TheDoor.GetComponent();"

The door should be opening by animation, yeah. I would be calling it when I hit the button. And enabling and disabling the animator was actually an accident- I didn’t notice I repeated the line.

I think you need to call it like this

StartCoroutine(OpenTheDoor ());

Please just watch the video I linked. This is Animator 101. You need to set up parameters and send the Animator values. Not turn it on and off…

I watched your video, it definitely helped me out. I actually figured it out, and the door works find now. Thanks!

1 Like