Search Unity

FindObjectOfType<T> does not contain a definition for 'Length'

Discussion in 'Scripting' started by imposter_syndrom_incarnated, Sep 18, 2021.

  1. imposter_syndrom_incarnated

    imposter_syndrom_incarnated

    Joined:
    May 1, 2020
    Posts:
    51
    have linked my game with mysql database and I am trying to make sure that the logged in user's data does not disappear once instantiated. I tagged the prefab (which gets instantiated every time the use logs in) of the script below as CurrentPlayer and wrote the following:


    Code (CSharp):
    1. public class CurrentPlayer : MonoBehaviour
    2. {
    3.    public string Username;
    4.    public string Email;
    5.    public int Score;
    6.  
    7.    private void Awake()
    8.    {
    9.        var players = FindObjectOfType<CurrentPlayer>();
    10.        if (players.Length > 1)
    11.        {
    12.            Destroy(gameObject);
    13.          
    14.        }
    15.        DontDestroyOnLoad(gameObject);
    16.    }
    17. }
    upload_2021-9-18_14-8-11.png
    The problem is that I get the error CS1061: 'CurrentPlayer' does not contain a definition for 'Length' and no accessible extension method 'Length' accepting a first argument of type 'CurrentPlayer' could be found (are you missing a using directive or an assembly reference?) I followed a tutorial in which the same code did not trigger any compilation errors with Length.

    Anybody so kind to hint why this might be happening?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    There is
    FindObjectOfType<T>()
    what you are using.

    Are you perhaps looking for
    FindObjectsOfType<T>()
    ?

    This is one reason not to use
    var
    because if you had typed the variable (as
    CurrentPlayer[]
    ) then the error would appear precisely where it is, at the function callsite. :)
     
  3. imposter_syndrom_incarnated

    imposter_syndrom_incarnated

    Joined:
    May 1, 2020
    Posts:
    51
    Yes! ahah thank you!!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Looking at what you're doing, this is a poor singleton implementation. I know there's like ten billion examples of this sort of construct, but they're all kinda unwieldly. I recommend using something like this:

    Simple Singleton (UnitySingleton):

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://gist.github.com/kurtdekker/775bb97614047072f7004d6fb9ccce30

    Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

    https://gist.github.com/kurtdekker/2f07be6f6a844cf82110fc42a774a625

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

    If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
     
  5. imposter_syndrom_incarnated

    imposter_syndrom_incarnated

    Joined:
    May 1, 2020
    Posts:
    51
    thank you for the suggestion! I will imporve the code accordingly !
     
    Kurt-Dekker likes this.