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. Dismiss Notice

(solved)I keep getting syntax errors relating to needing parenthesis

Discussion in 'Scripting' started by Pixelated_Lagg, Apr 15, 2021.

  1. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    I am trying to make a script for muting/unmuting music with a button. I am new to Unity and C# so I probably made a rookie mistake, but here is my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class MusicVolume : MonoBehaviour
    {
    public void MenuMusicVolume()
    {
    if audioSource.mute == false;
    {
    audioSource.mute = true;
    }

    if audioSource.mute == true;
    {
    audioSource.mute = false;
    }
    }
    }

    Any help is greatly appreciated.
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
  3. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    Thanks for the help
     
  4. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    Please use 'Insert Code' option when posting code here and make sure the code is well formatted:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MusicVolume : MonoBehaviour
    6. {
    7.     public void MenuMusicVolume()
    8.     {
    9.         if audioSource.mute == false;
    10.         {
    11.             audioSource.mute = true;
    12.         }
    13.  
    14.         if audioSource.mute == true;
    15.         {
    16.             audioSource.mute = false;
    17.         }
    18.     }
    19. }
    Also posting the actual error messages will make it much easier for people to help you.

    The parenthesis semicolons you have on if statement lines shouldn't be there.
    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/if-else
     
    Last edited: Apr 15, 2021
    Schneider21 likes this.
  5. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    One way to simplify the code:
    Code (CSharp):
    1. public void MenuMusicVolume()
    2. {
    3.     if (audioSource.mute == false)
    4.     {
    5.         audioSource.mute = true;
    6.     }
    7.  
    8.     else
    9.     {
    10.         audioSource.mute = false;
    11.     }
    12. }
    We can simplify it further:
    Code (CSharp):
    1. public void MenuMusicVolume()
    2. {
    3.     audioSource.mute = !audioSource.mute;
    4. }
    https://docs.microsoft.com/en-us/do...-logical-operators#logical-negation-operator-
     
  6. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    I've encountered another syntax error saying I need to add a comma:
    Assets\Mainmenu Assets\MusicVolume.cs(9,39): error CS1003: Syntax error, ',' expected
    Heres my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MusicVolume : MonoBehaviour
    6. {
    7.     void start()
    8.     {
    9.         gameObject.GetComponent(Audio Source);
    10.     }
    11.  
    12.     public void MenuMusicVolume()
    13.     {
    14.         if (audioSource.mute == false);
    15.         {
    16.             audioSource.mute = true;
    17.         }
    18.  
    19.         if (audioSource.mute == true);
    20.         {
    21.             audioSource.mute = false;
    22.         }
    23.     }
    24. }
    25.  
     
  7. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    Remove the semi colons on 14 and 19. Although I understand why you thought to put them there, that's not how it works with if statements :)

    Also, I know you didn't ask, but capitalise that 's' on 7. Start() is the function Unity calls automatically, start() is valid, but you will have to call it manually and I don't think you intended that.
     
  8. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    Right. Sorry, I confused things a little bit. When talking about parenthesis before I was thinking semicolons - terribly tired right now.
    You fixed the parenthesis but now you have some extra semicolons that shouldn't be there at end of if statement lines (14 and 19).
     
  9. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    After deleting the semi colons, it still gives me the error.
     
  10. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    GetComponent<> not GetComponent() on 9. AudioSource is probably supposed to be one word too.
     
  11. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    Adding angle brackets doesn't fix the error either.
     
  12. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    Your incorrect method call here is the source of this error:
    Code (CSharp):
    1. GetComponent(Audio Source);
     
  13. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    Looks like you're getting way ahead of yourself. You should probably learn some C# basics before trying to apply that knowlage in Unity.
     
  14. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    Deleting gameObject. doesn't fix the error either. And I was hoping that making a game would help me learn C# better than watching a mind-numbing tutorial on Youtube.
     
  15. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    Here's what you were probably going for:
    Code (CSharp):
    1. using UnityEngine;
    2. public class MusicVolume : MonoBehaviour
    3. {
    4.     AudioSource audioSource = null;
    5.  
    6.     void start()
    7.     {
    8.         audioSource = gameObject.GetComponent<AudioSource>();
    9.     }
    10.  
    11.     public void MenuMusicVolume()
    12.     {
    13.         if (audioSource.mute == false)
    14.         {
    15.             audioSource.mute = true;
    16.         }
    17.         if (audioSource.mute == true)
    18.         {
    19.             audioSource.mute = false;
    20.         }
    21.     }
    22. }
    Simplified:
    Code (CSharp):
    1. using UnityEngine;
    2. public class MusicVolume : MonoBehaviour
    3. {
    4.     AudioSource audioSource = null;
    5.  
    6.     void start()
    7.     {
    8.         audioSource = gameObject.GetComponent<AudioSource>();
    9.     }
    10.  
    11.     public void MenuMusicVolume()
    12.     {
    13.         audioSource.mute = !audioSource.mute;
    14.     }
    15. }
     
  16. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    That fixed the error. Thanks!
     
  17. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,921
    Instead of a game go with simpler stuff. Make a number count up from 0. Then count down from 10000. Then make it go from 0 to 100 over and over. Then make a cube move from x=0 to x=10 is small steps so it takes about 2 seconds. Make it go from black to white, slowly, over and over. Make the red, green and blue change at different rates so the colors go through most of the rainbow over time. Make typing "z" teleport a cube to a random spot on the screen.

    You should get lots of easier-to-fix errors and get all of that semicolon, curly-brace, not initialized ... stuff out of the way.
     
    Kurt-Dekker likes this.
  18. RWKeska

    RWKeska

    Joined:
    Apr 15, 2021
    Posts:
    24
    You cannot learn programming and a language like C# by watching a single tutorial on YT no matter how long. It's more like you pickup a series of tutorials or a book and then chapter by chapter you learn about more concepts. As you go you apply those concepts in your exercise projects to actually learn them instead of about them. Some dozens or couple hundred hours later you will have enough basic knowledge about programming to then start moving towards an advanced application like game programming.

    It is certainly possible to learn programming, C# and Unity at the same time but that just adds complexity and means more theory before getting hands on experience. Try searching for some tutorials like "Learn programming with Unity" or some such.
     
    Last edited: Apr 15, 2021
  19. Pixelated_Lagg

    Pixelated_Lagg

    Joined:
    Jan 23, 2021
    Posts:
    35
    That sounds like it would help with syntax errors, but I like to have an eventual goal to achieve - such as creating a game.
     
  20. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    No, you're completely missing the point. Learning how to fix syntax errors is a tiny trivial side effect. Anybody can teach a monkey how to type properly. That's not useful or even noteworthy.

    What @Owen-Reynolds suggests will help your brain develop the patterns and internal thinking models required to make functioning interactive software, and give you copious examples of iterative development and guide you to understand how game software actually works and is put together. I'm not exaggerating that.