Simple coding issue I am not equipped for. Please Help!

I am an art student and have no idea how to do computers good. My professor and I have been trying to get my animations to be triggered by key presses. I have a character who needs to be able to shift from their idle animation to their walking animation. I am also adding an additional animation where my character will hop into the air and hover. I would love for this to be a trigger that you hold the SpaceBar down to hover and let go to stop hovering. However, we settled with a space bar to enter hover and the “F” key to toggle hover off. I have my animater already set up and seems to be working fine as is. I have 5 animations in total:

“Idle” = Character is in an idle state
“Walk” = Character walks
“Flight” = Character enters the flight
“Flight State” = Character is in an idle state but is hovering
“Landing” = Character exits flight to return to idle


The Errors I am getting are:

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Assets\FlightChracterAnim.cs(54,6): error CS1513: } expecte


Here is the code I have:

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

public class FlightCharacterAnim : MonoBehaviour
{
public Animator animator;
public float speed = 100f;
public int grounded = 1;

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
grounded = 0;
animator.SetBool(“Walk”, false);
}
if (Input.GetKeyDown(KeyCode.F))
{
grounded = 1;
animator.SetBool(“Flight”, false);
}
if ((Input.GetAxis(“Horizontal”) != 0) & (grounded = 1))
{
animator.SetBool(“Walk”, true);
transform.Translate(Vector3.right * Time.deltaTime * speed);
if (Input.GetAxis(“Horizontal”) < 0)
{
transform.rotation = Quaternion.Euler(0, 180, 0);
}
if (Input.GetAxis(“Horizontal”) > 0)
{
transform.rotation = Quaternion.Euler(0, 0, 0);
}
}
else
{
animator.SetBool(“Walk”, false);
}
if ((Input.GetAxis(“Horizontal”) != 0) & (grounded = 0))
{
animator.SetBool(“Flight”, true);
transform.Translate(Vector3.right * Time.deltaTime * speed);
if (Input.GetAxis(“Horizontal”) < 0)
{
transform.rotation = Quaternion.Euler(0, 180, 0);
}
if (Input.GetAxis(“Horizontal”) > 0)
{
transform.rotation = Quaternion.Euler(0, 0, 0);
}
}
}
}

Welcome! If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

Line 55 needs to be a closing curly brace.

That brace would close out the class. See how the top public class declaration has an opening brace and there is no closing brace?

The argument out of range exception error is most likely not from the above code, but here’s some info on it:

Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

http://plbm.com/?p=236

In general, you actually CAN figure this stuff out yourself, and here’s some more tips on that:

How to understand compiler and other errors and even fix them yourself:

3 Likes

You have 1 less closing brackets than opening brackets. The error message says line 54, that’s at the very end of your script. You will notice if you look there that you never close the class out. On line 6 you have an opening bracket, and I’d expect a closing bracket around line 55. The error message says line 54, because that’s the last line in the script you have any text. But generally, if the error message doesn’t point to the exact line where the problem is caused, the problem is on an adjacent line.

To further explain, in this case the issue is resolved literally by what the error message says (this is often the case, but sometimes requires a bit more thinking than here). The error message is “} expected”, because the problem is exactly that. A closing } was expected there, and doesn’t exist. So put one there.

Lastly, Kurt’s point about posting code with CODE tags in the forum is important. (I also just noticed he already answered the question and I’m just restating it, but oops). I only could see the issue in your code because of the screen shot of VS, but often scripts are way too long for that. Your unformatted code pasted in the forum without CODE tags make counting to line 55 too big a chore to reasonably ask people to do for you for free. Hope you’re having fun with Unity, good luck to you!

2 Likes

I appreciate the help, but now I’m running into an issue that I don’t quite understand. If not an overt solution I would at least like to know what I should be looking at to remedy this problem. Am I just naming something wrong? Am I using incorrect notation? I am not a coder by any means and I don’t really understand anything about coding.

Here is the error I am getting:

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

public class FlightCharacterAnim : MonoBehaviour
{
    public Animator animator;
    public float speed = 100f;
    public int grounded = 1;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            grounded = 0;
            animator.SetBool("Walk", false);
        }
        if (Input.GetKeyDown(KeyCode.F))
        {
            grounded = 1;
            animator.SetBool("Flight", false);
        }
        if ((Input.GetAxis("Horizontal") != 0) & (grounded = 1))
        {
            animator.SetBool("Walk", true);
            transform.Translate(Vector3.right * Time.deltaTime * speed);
            if (Input.GetAxis("Horizontal") < 0)
            {
                transform.rotation = Quaternion.Euler(0, 180, 0);
            }
            if (Input.GetAxis("Horizontal") > 0)
            {
                transform.rotation = Quaternion.Euler(0, 0, 0);
            }
        }
        else
        {
            animator.SetBool("Walk", false);
        }
        if ((Input.GetAxis("Horizontal") != 0) & (grounded = 0))
        {
            animator.SetBool("Flight", true);
            transform.Translate(Vector3.right * Time.deltaTime * speed);
            if (Input.GetAxis("Horizontal") < 0)
            {
                transform.rotation = Quaternion.Euler(0, 180, 0);
            }
            if (Input.GetAxis("Horizontal") > 0)
            {
                transform.rotation = Quaternion.Euler(0, 0, 0);
            }
        }
    }
}

From within your if statement, the term

grounded = 1

actually ASSIGNS the value 1 to grounded and has an implicit result type of integer.

You cannot feed that into the “logical and (&)” operator along with a bool. That’s the complaint.

Pretty sure you want the comparison operator:

grounded == 1

Remember, every single character counts and cannot be omitted.

ALSO… if grounded is only used to either be 0 or 1, make it a bool

2 Likes

Thanks! This has solved the issue. However, now I’m being hit with this error: 6876641--803042--Unity_Code_error_2.png
From what I can gather, this error isn’t specific. I don’t know where to add a comma if that is the issue and if I’m missing curly brackets somewhere.

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

public class FlightCharacterAnim : MonoBehaviour
{
    public Animator animator;
    public float speed = 100f;
    public bool grounded == 1;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            grounded = 0;
            animator.SetBool("Walk", false);
        }
        if (Input.GetKeyDown(KeyCode.F))
        {
            grounded = 1;
            animator.SetBool("Flight", false);
        }
        if ((Input.GetAxis("Horizontal") != 0) & (grounded = 1))
        {
            animator.SetBool("Walk", true);
            transform.Translate(Vector3.right * Time.deltaTime * speed);
            if (Input.GetAxis("Horizontal") < 0)
            {
                transform.rotation = Quaternion.Euler(0, 180, 0);
            }
            if (Input.GetAxis("Horizontal") > 0)
            {
                transform.rotation = Quaternion.Euler(0, 0, 0);
            }
        }
        else
        {
            animator.SetBool("Walk", false);
        }
        if ((Input.GetAxis("Horizontal") != 0) & (grounded = 0))
        {
            animator.SetBool("Flight", true);
            transform.Translate(Vector3.right * Time.deltaTime * speed);
            if (Input.GetAxis("Horizontal") < 0)
            {
                transform.rotation = Quaternion.Euler(0, 180, 0);
            }
            if (Input.GetAxis("Horizontal") > 0)
            {
                transform.rotation = Quaternion.Euler(0, 0, 0);
            }
        }
    }
}

Single = for assignment, double == for comparison. You’re using the wrong one on line 9 (which is confusing the compiler so much it doesn’t even know what you intended).

Alright, I think I understand. with these errors I am just looking at changing my code on these specific lines from true or false to 1 or 0?:

6876710--803060--Unity_Code_error_3.png

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

public class FlightCharacterAnim : MonoBehaviour
{
    public Animator animator;
    public float speed = 100f;
    public bool grounded = 1;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            grounded = 0;
            animator.SetBool("Walk", false);
        }
        if (Input.GetKeyDown(KeyCode.F))
        {
            grounded = 1;
            animator.SetBool("Flight", false);
        }
        if ((Input.GetAxis("Horizontal") != 0) & (grounded = 1))
        {
            animator.SetBool("Walk", true);
            transform.Translate(Vector3.right * Time.deltaTime * speed);
            if (Input.GetAxis("Horizontal") < 0)
            {
                transform.rotation = Quaternion.Euler(0, 180, 0);
            }
            if (Input.GetAxis("Horizontal") > 0)
            {
                transform.rotation = Quaternion.Euler(0, 0, 0);
            }
        }
        else
        {
            animator.SetBool("Walk", false);
        }
        if ((Input.GetAxis("Horizontal") != 0) & (grounded = 0))
        {
            animator.SetBool("Flight", true);
            transform.Translate(Vector3.right * Time.deltaTime * speed);
            if (Input.GetAxis("Horizontal") < 0)
            {
                transform.rotation = Quaternion.Euler(0, 180, 0);
            }
            if (Input.GetAxis("Horizontal") > 0)
            {
                transform.rotation = Quaternion.Euler(0, 0, 0);
            }
        }
    }
}

The error is EXTREMELY specific. That (9,26) means “Line 9, 26 characters over.”

As Ray points out above, that’s the issue.

I will post this again, since you seem to have missed it: How to understand compiler and other errors and even fix them yourself:

https://forum.unity.com/threads/ass…3-syntax-error-expected.1039702/#post-6730855

Way back in oldskool days 0 and 1 were used to indicate false and true because a) we didn’t have a bool type, and b) it maps to what the underlying CPU is doing anyway.

When we see you have this construct in your code, we all assume you know why it’s there, that you have oldskool experience with C-style languages that would lead you do make such a quaint and anachronistic thing as a boolean quantity via integer 0 and 1.

Today in C# we tend to use bools and false/true. C# is strongly typed, which means you can’t stick anything else except the value true or the value false in a bool. Likewise you cannot stick true/false in an integer, or compare an integer to a bool (see 7 or 8 posts up.)

If you got this code somewhere, it was either a) completely broken, or b) you typed it in wrong. Either way, it is at least slightly unusual to use a 0 and 1 for false/true in the year 2021 in C#.

If you are following a tutorial, DO NOT DEVIATE from that. Not even by one character.

Either way, you absolutely will get nowhere just dabbling around with random combinations. Follow tutorials until you can understand 100% of the parts involved, and only then actually contemplate doing your own thing with them.

if ((Input.GetAxis("Horizontal") != 0) & (grounded = 1))

should be

if ((Input.GetAxis("Horizontal") != 0) && (grounded == 1))

Note the double == as well as the double &&. Read up on conditionals and their syntax, they are essential!

bools are true or false only.
Also…if you are coding, you have at least started down that path…so don’t say “I’m not a coder”…You are! And even though it can be very frustrating, it is also very rewarding…stick with it! :slight_smile:
It takes awhile to get used to even basic syntax, but it will come, and you’ll start being able to spot errors more and more easily.

1 Like

Thanks a bunch! I got it working finally.

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

public class FlightCharacterAnim : MonoBehaviour
{
    public Animator animator;
    public float speed = 100f;
    public bool grounded;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            grounded = false;
            animator.SetBool("Walk", false);
            animator.SetBool("Flight", true);
        }
        if (Input.GetKeyDown(KeyCode.F))
        {
            grounded = true;
            animator.SetBool("Flight", false);
        }
        if ((Input.GetAxis("Horizontal") != 0) & (grounded = true))
        {
            animator.SetBool("Walk", true);
            transform.Translate(Vector3.right * Time.deltaTime * speed);
            if (Input.GetAxis("Horizontal") < 0)
            {
                transform.rotation = Quaternion.Euler(0, 180, 0);
            }
            if (Input.GetAxis("Horizontal") > 0)
            {
                transform.rotation = Quaternion.Euler(0, 0, 0);
            }
        }
        else
        {
            animator.SetBool("Walk", false);
        }
        if ((Input.GetAxis("Horizontal") != 0) & (grounded = false))
        {
            transform.Translate(Vector3.right * Time.deltaTime * speed);
            if (Input.GetAxis("Horizontal") < 0)
            {
                transform.rotation = Quaternion.Euler(0, 180, 0);
            }
            if (Input.GetAxis("Horizontal") > 0)
            {
                transform.rotation = Quaternion.Euler(0, 0, 0);
            }
        }
    }
}

So you know,

If (grounded = false)

and

if (grounded = true)

will always evaluate as true. As mentioned, we believe you mean

if (grounded == false) [equality not assignment)

1 Like

Agreed—make the changes so it’s using == and &&. Otherwise it’s “working”, but it’s still not logical, and it’s only working by luck.

2 Likes