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

Question Unexpected format error

Discussion in 'Scripting' started by GenericFlicker, May 17, 2023.

  1. GenericFlicker

    GenericFlicker

    Joined:
    Oct 9, 2022
    Posts:
    3
    So i'm following a tutorial for creating a state machine to control my player characters in my project. The problem is i'm getting a CS1003 syntax error on my state runner even though the state runner is the one part i didn't change and no error was spotted in the video. Not sure what's going on.

    The error is Syntax error, ',' expected on line 17 twice CS1003
    and two counts of the name 's' does not exist in the currect context on line 17, but even adding s as a variable while fixing that second error does not fix the first syntax error.


    Code (CSharp):
    1. using Player.States;
    2. using System;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. namespace Player
    7. {
    8.     public abstract class StateRunner<T> : MonoBehaviour where T : MonoBehaviour
    9.     {
    10.         [SerializeField]
    11.         private List<State<T>> _states;
    12.         private readonly Dictionary<Type, State<T>> _stateByType = new();
    13.         private State<T> _activeState;
    14.         private State<T> s;
    15.  
    16.         protected virtual void Awake()
    17.         {
    18.             _states.ForEach(s: State<T> => _stateByType.Add(s.GetType(), s));
    19.             SetState(_states[0].GetType());
    20.         }
    21.  
    22.         public void SetState(Type newStateType)
    23.         {
    24.             if (_activeState != null)
    25.             {
    26.                 _activeState.Exit();
    27.             }
    28.  
    29.             _activeState = _stateByType[newStateType];
    30.             _activeState.Init(parent: GetComponent<T>());
    31.         }
    32.  
    33.         private void Update()
    34.         {
    35.             _activeState.CaptureInput();
    36.             _activeState.Update();
    37.             _activeState.ChangeState();
    38.         }
    39.  
    40.         private void FixedUpdate()
    41.         {
    42.             _activeState.FixedUpdate();
    43.         }
    44.     }
    45. }
     
    Last edited: May 17, 2023
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    You haven't linked us the tutorial to compare to, but List<T>.Foreach does not have named parameter 's'.

    So you haven't followed the tutorial properly.
     
    Nad_B likes this.
  3. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    303
    It should be
    _states.ForEach(s => _stateByType.Add(s.GetType(), s));
     
    Bunny83 and spiney199 like this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    My guess is that he was looking at the code through an IDE like Rider which implicitly displays the type of variables behind them with a colon. This is not part of the code. That's why using Rider for tutorials is a bad idea because beginners have no idea what they're looking at.
     
    Nad_B and spiney199 like this.
  5. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,057
    it works with method calls though.

    This is valid code.
    Code (CSharp):
    1. var list = new List<string>();
    2. HandleList(myList: list);
    3.  
    4. private void HandleList(List<string> myList) {}
    But as spiney already mentioned, there is no
    List<T>.ForEach
    with a parameter called
    s
    .
    Could it be an extension method maybe?

    edit: Yeah I got confused, my bad.
     
    Last edited: May 17, 2023
  6. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,495
    No, what you think of are named parameters. He has a type behind the colon, not a value. That's what Rider is showing you behind a variable. So the code is perfectly valid (ignoring the type that rider shows you). Nad_B posted the actual correct code. So simply a lambda expression and "s" is just the argument of the lambda expression. Rider shows you the implicitly derived type, however you can not / must not type that in. You can use explicitly typed arguments in a lambda expression, however in that case you have to use

    Code (CSharp):
    1. _states.ForEach((State<T> s) => _stateByType.Add(s.GetType(), s));
    This is the same as

    Code (CSharp):
    1. _states.ForEach(s => _stateByType.Add(s.GetType(), s));
    or
    Code (CSharp):
    1. _states.ForEach((s) => _stateByType.Add(s.GetType(), s));
     
    MaskedMouse and Nad_B like this.
  7. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    303
    Rider displays variable types (grayed) if the variable name is different from the type (I think they call it "inline hints")
    upload_2023-5-17_13-59-38.png

    upload_2023-5-17_14-3-31.png

    Which I find very helpful, but as Bunny said can confuse beginners.
     
    MaskedMouse and Bunny83 like this.
  8. GenericFlicker

    GenericFlicker

    Joined:
    Oct 9, 2022
    Posts:
    3
    you guys called it actually, the tutorial is using rider while I'm on visual studio and I absolutely was confused by what I was looking at. Removing those bits where rider was showing the typing got rid of the error np. Code still isn't working but I'm attempting to troubleshoot that myself first to see if I can find where I goofed this time.

    edit: managed to get it working but noticing some rather choppy frames considering there's only a player on a platform so that's weird

     
    Last edited: May 18, 2023