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

Question FirstOrDefault not working even with System.Linq;

Discussion in 'Scripting' started by jawsome290, Feb 11, 2023.

  1. jawsome290

    jawsome290

    Joined:
    Jan 20, 2023
    Posts:
    3
    I've got no idea what going on, I followed a tutorial from a couple years ago to setup local multiplayer, however I already had a project made so I've been modifying existing code to add the tutorial code. Because of this I might not have imported an asset or package I needed.


    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using UnityEngine;
    using UnityEngine.InputSystem;

    public class PlayerInputHandler : MonoBehaviour
    {
    private PlayerInput playerInput;
    private Player1 player1;
    // Start is called before the first frame update
    private void Awake()
    {
    playerInput = GetComponent<PlayerInput>();
    var player1s = FindObjectOfType<Player1>();
    var index = playerInput.playerIndex;
    player1 = player1s.FirstOrDefault(p => p.GetPlayerIndex == index);
    }

    // Update is called once per frame
    void Update()
    {

    }
    }

    the FirstOrDefault has the error 'Player1' does not contain a definition for 'FirstOrDefault' and no accessible extension method 'FirstOrDefault' accepting a first argument of type 'Player1' could be found.

    Thanks
     
  2. jawsome290

    jawsome290

    Joined:
    Jan 20, 2023
    Posts:
    3
    well I found the fix and its just as stupid as you might think.
    Change 'FindObjectOfType' to 'FindObjectsOfType'
    and that's it. AutoComplete can be a b**** sometimes lol
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,622
    Your post has nothing to do with 2D physics or 2D so please put such posts in the scripting forum.

    I'll move your post for you.
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,572
    That's the potential risk of using "var" as it may hide issues that would resurface further down the road. In your case because you used
    FindObjectOfType
    instead of
    FindObjectsOfType
    the variable
    var player1s
    would be automatically of type
    Player1
    instead of
    Player1[]
    . Typing out the type explicitly would already cause an error in that line. The
    var
    simply adapted to the "wrong" return type so the Linq methods of course failed since the variable was not a collection.

    Don't get me wrong. I also use var in such cases. However, especially when debugging issues, you should be sure about what type you're expecting. In Visual Studio you can hover the var keyword with your mouse and the tooltip will tell you the actual type.