Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Member cannot be accessed with an instance reference, but qualifying with a type name throws errors

Discussion in 'Scripting' started by trollzor45, Jan 11, 2024.

  1. trollzor45


    Jan 8, 2023
    I'm using a function calling a SpriteRenderer to show a small button notification above my character, implying that something will happen if you press said button.

    However, the way I implemented it (the same way Unity Docs does it, by declaring a Sprite list) means that I would have to refer to the sprites I want to display by index number in my list (called keyList) instead of filename.

    I would infinitely prefer to give my function a char instead of an index number for readability. I try to do this with
    Array.IndexOf(keyList, key)
    so I can get the index of the char-length file name, but using "Array" tells me that "Array" does not exist in this current context...

    So I try
    keyList.IndexOf(keyList, key)
    but THAT throws "Member cannot be accessed with an instance reference" and now I'm stuck.

    When I qualify the line with a type name, it says the type name doesn't exist, and when I use the variable name, it tells me to use the type name! :(

    This is (the important) parts of my code for refrence.

    Code (CSharp):
    1. private Sprite[] keyList;
    2. ...
    4. ...
    5. void Start()
    6. {
    7.         keyList = Resources.LoadAll<Sprite>("Sprites/Player/Keys");
    8. }
    9. ...
    11. ...
    12. public void ShowKey(bool show, char key){
    13.         if (show == false){
    14.             interactSpriteRenderer.sprite = null;
    15.         }
    16.         else if (show == true){
    17.            interactSpriteRenderer.sprite = keyList[keyList.IndexOf(keyList, key)]; //throws "instance refrence" error
    18.         }
    19.     }
  2. spiney199


    Feb 11, 2021
    I think you were missing a
    using System;
    for the first issue you had. The
    class is in the System namespace.
    trollzor45 likes this.
  3. trollzor45


    Jan 8, 2023
    This worked! Thank you :)
    spiney199 likes this.