Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Unexpected symbol "("

Discussion in 'Scripting' started by Code1345, Aug 10, 2017.

  1. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    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;
    }
    }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    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. https://forum.unity3d.com/threads/using-code-tags-properly.143875/

    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.
     
  3. carl010010

    carl010010

    Joined:
    Jul 14, 2010
    Posts:
    139
  4. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    You had an extra closing curley:
    Code (CSharp):
    1. // You had one too many closing curley brackets
    2. // Improvement: Cache the component so you dont overuse GetComponent
    3. // Improvement: Cache WaitForSeconds to reduce garbage generation
    4.  
    5. void OpenTheDoor ()
    6. {
    7.     Animator anim = TheDoor.GetComponent<Animator>();
    8.     WaitForSeconds shortWait = new WaitForSeconds(1f);
    9.  
    10.     anim.enabled = true;
    11.  
    12.     yield return shortWait;
    13.     anim.enabled = false;
    14.  
    15.     yield return new WaitForSeconds(5);
    16.     anim.enabled = true;
    17.  
    18.     yield return shortWait;
    19.     anim.enabled = false;
    20. }
    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.
     
  5. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    I used your code and it worked great! Thank you so much.
     
    TaleOf4Gamers likes this.
  6. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    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!
     
  7. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    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!
     
  8. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class OpenDoor001 : MonoBehaviour {
    6.  
    7.  
    8. public GameObject TextDisplay;
    9. public float TheDistance = PlayerRaycast.DistanceFromTarget;
    10. public GameObject TheDoor;
    11.  
    12.  
    13.  
    14. void  Update (){
    15.     TheDistance = PlayerRaycast.DistanceFromTarget;
    16.     if (Input.GetButtonDown("Action")) {
    17.         if (TheDistance <= 2) {
    18.             OpenTheDoor();
    19.  
    20.         }
    21.     }
    22.      
    23. }
    24.  
    25. void  OnMouseOver (){
    26.     if (TheDistance <= 2) {
    27.         TextDisplay.GetComponent<Text>().text = "Press Button";
    28.     }
    29. }
    30.  
    31. void  OnMouseExit (){
    32.     TextDisplay.GetComponent<Text>().text = "";
    33. }
    34.  
    35. IEnumerator OpenTheDoor ()
    36. {
    37.     Animator anim = TheDoor.GetComponent<Animator>();
    38.     WaitForSeconds shortWait = new WaitForSeconds(1f);
    39.     anim.enabled = true;
    40.     yield return shortWait;
    41.     anim.enabled = false;
    42.     yield return new WaitForSeconds(5);
    43.     anim.enabled = true;
    44.     yield return shortWait;
    45.     anim.enabled = false;
    46. }
    47. }
    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!
     
  9. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    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.
     
  10. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.

    https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html

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

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    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.
     
  12. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    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.
     
  13. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    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.
     
  14. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    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
     
  15. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    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 beat me to questioning about the animator.
     
  16. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61

    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<Animator>();"
     
  17. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    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.
     
  18. Simo

    Simo

    Joined:
    Sep 16, 2012
    Posts:
    85
    I think you need to call it like this

    StartCoroutine(OpenTheDoor ());
     
  19. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    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...
     
  20. Code1345

    Code1345

    Joined:
    May 21, 2017
    Posts:
    61
    I watched your video, it definitely helped me out. I actually figured it out, and the door works find now. Thanks!
     
    TaleOf4Gamers likes this.