Search Unity

Error CS1026: ) expected

Discussion in 'Scripting' started by GageMonke, Aug 11, 2022.

  1. GageMonke

    GageMonke

    Joined:
    Aug 11, 2022
    Posts:
    7
    Hi, so I am having this problem where my script is having the error code above, and I can't seem to fix it. Visual Studio 2022 (what I used to code the script), says nothing is wrong, but that error keeps showing up. I am attempting to create a VR game soon and would appreciate some feedback as soon as possible. Heres the code.


    using UnityEngine;

    public class PhysicsHand : MonoBehaviour
    {
    [Header("PID")]
    [SerializeField] float frequency = 50f;
    [SerializeField] float damping = 1f;
    [SerializeField] float rotFrequency = 100f;
    [SerializeField] float rotDamping = 0.9f;
    [SerializeField] Rigidbody playerRigidbody;
    [SerializeField] Transform target;
    Rigidbody _rigidbody;
    void Start()
    {
    _rigidbody = GetComponent<RigidBody>();
    _rigidbody.maxAngularVelocity = float.PositiveInfinity;
    }


    void FixedUpdate()
    {
    PIDMovement();
    PIDRotation();
    }
    void PIDMovement()
    {
    float kp = (6f * frequency) * (6f * frequency) * 0.25f;
    float kd = 4.5f * frequency * damping;
    float g = 1 / (1 + kd * Time.fixedDeltaTime + kp * Time.fixedDeltaTime * Time.fixedDeltaTime);
    float ksg = kp * g;
    float kdg = (kd * kp * Time.fixedDeltaTime) * g;
    Vector3 force = (target.position - transform.position) * ksg + (playerRigidbody.velocity - _rigidbody.velocity) * kdg;
    _rigidbody.AddForce(force, Forcemode.Acceleration);
    }

    void PIDRotation()
    {
    float kp = (6f * rotFrequency) * (6f * rotFrequency) * 0.25f;
    float kd = 4.5f * rotFrequency * rotDamping;
    float g = 1 / (1 + kd * Time.fixedDeltaTime + kp * Time.fixedDeltaTime * Time.fixedDeltaTime;
    float ksg = kp * g;
    float kdg = (kd + kp * Time.fixedDeltaTime) * g;
    Quaternion q = target.rotation * Quaternion.Inverse(transform.rotation);
    if (q.w < 0)
    {
    q.x = -q.x;
    q.y = -q.y;
    q.z = -q.z;
    q.w = -q.w;
    }
    q.ToAngleAxis(out float angle, out Vector3 axis);
    axis.Normalize();
    axis *= Mathf.Deg2Rad;
    Vector3 torque = ksg * axis * angle + -_rigidbody.angularVelocity * kdg;
    _rigidbody.AddTorque(torque, Forcemode.Acceleration);
    }



    It's long, but I really need some help.
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    When posting code on this forum, please use Code-Tags. It makes it easier to read, which makes it easier for people to help you.

    Visual Studio telling you there's nothing wrong means it's not properly configured for Unity, and you probably want to look up how to do that. You're missing a parenthesis on this line.

    Code (CSharp):
    1. void PIDRotation()
    2. {
    3.     float kp = (6f * rotFrequency) * (6f * rotFrequency) * 0.25f;
    4.     float kd = 4.5f * rotFrequency * rotDamping;
    5.     float g = 1 / (1 + kd * Time.fixedDeltaTime + kp * Time.fixedDeltaTime * Time.fixedDeltaTime; // <==
    6.     float ksg = kp * g;
    7.     float kdg = (kd + kp * Time.fixedDeltaTime) * g;
    8.     // etc.
    9.  
     
  3. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Also, these errors are extremely common and easy to Google. Not only that, the errors usually tell you which line has the problem, which you should include in future posts, or just look at yourself. If you double-click the error in Unity, it will take you straight to it.
     
  4. GageMonke

    GageMonke

    Joined:
    Aug 11, 2022
    Posts:
    7
    Sorry for my lack of Code-Tags, I'm new and don't really know how the forum works. That suggestion works, but now I'm faced with 2 of the error CS0103: The name 'Forcemode' does not exist in the current context, and Error CS0246: The namespace name 'RigidBody' could not be found. I'm following a tutorial, and I'm not sure if it is meant to be lowercase, I typed it wrong, or it's just not the right phrase in the context. The thing I find odd is that in the tutorial, unity and visual studio take Forcemode and RigidBody normally, I might not have it set up right, but you can find Force mode in the last paragraph, and the 3rd to last.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Look very carefully at what the tutorial did. You didn't do it the same way. C# is case sensitive.
     
  6. GageMonke

    GageMonke

    Joined:
    Aug 11, 2022
    Posts:
    7
    When I double click on the error, it just sends me to the script, and dosent show me where it is. Visual Studio also won't show the line numbers, despite me attempting to search it up and find it.
     
  7. GageMonke

    GageMonke

    Joined:
    Aug 11, 2022
    Posts:
    7
    I'll look through it again.
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Attention to detail...
    Rigidbody? Or RigidBody?
    Forcemode? Or ForceMode?

    Only one of these is correct.
     
  9. GageMonke

    GageMonke

    Joined:
    Aug 11, 2022
    Posts:
    7
    That fixed everything, Thank you.
    As this is one of my first scripts, (and my general lack of first time attention), I missed those. Thank you!
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    Follow this to be successful:

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors...

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.