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 are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

[SOLVED] Member cannot be accessed with an instance reference; qualify it with a type name instead

Discussion in 'Scripting' started by ProExCurator, Mar 9, 2020.

Thread Status:
Not open for further replies.
  1. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    Code (CSharp):
    1. public class CreditsButton : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     public GameObject newGameButton;
    5.    
    6.     public GameObject[] mainMenu;
    7.     public GameObject[] credits;
    8.    
    9.     public GameObject settingsButton;
    10.     public SettingsButton settingsScript;
    11.    
    12.     public Image newGameButtonSprite;
    13.    
    14.     void Start()
    15.     {
    16.         newGameButtonSprite = newGameButton.GetComponent<Image>();
    17.         settingsButton = GameObject.Find("Settings");
    18.         settingsScript = settingsButton.GetComponent<SettingsButton>();
    19.         mainMenu = GameObject.FindGameObjectsWithTag("MainMenuTag");
    20.         settingsScript.mainMenuRef = mainMenu;
    21.         credits = GameObject.FindGameObjectsWithTag("CreditsTag");
    22.         foreach(GameObject credit in credits)
    23.         {
    24.             credit.SetActive(false);
    25.         }
    26.     }
    27. ...
    28.  
    Code (CSharp):
    1. public class SettingsButton : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     public GameObject newGameButton;
    5.    
    6.     public Image newGameButtonSprite;
    7.    
    8.     public static GameObject[] mainMenuRef;
    9.     public GameObject[] settings;
    10.    
    11.     void Start()
    12.     {
    13.         newGameButtonSprite = newGameButton.GetComponent<Image>();
    14.         settings = GameObject.FindGameObjectsWithTag("SettingsTag");
    15.         foreach(GameObject setting in settings)
    16.         {
    17.             setting.SetActive(false);
    18.         }
    19.     }
    20. ...
    21.  
    How can I do it like I want to?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    6,757
    mainMenuRef is static, so you can't access it through an instance. I don't know if you really intend for it to be static, but either remove the static or access it like this

    Code (CSharp):
    1. SettingsButton.mainMenuRef = mainMenu;
     
    ProExCurator likes this.
  3. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    That was it. Thank you ;)
     
    MuhammadAhmadYousaf likes this.
  4. momomcjol

    momomcjol

    Joined:
    Mar 22, 2020
    Posts:
    1
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class PlayerMovement : MonoBehaviour
    {
    public CharacterController controller;
    public float gravity = 9.81f;
    public float speed = 12f;
    public Transform groundCheck;
    public float grouondDistance = 0.4f;
    public LayerMask groundMask;
    public Physics Physiscs;

    Vector3 velocity;
    bool isGrounded;
    // Update is called once per frame
    void Update()
    {
    isGrounded = Physiscs.CheckSphere(groundCheck.position, grouondDistance, groundMask);
    if(isGrounded && velocity.y < 0)
    {
    velocity.y = -2f;
    }
    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");
    Vector3 move = transform.right * x + transform.forward * z;
    controller.Move(move * speed * Time.deltaTime);
    velocity.y -= gravity * Time.deltaTime;
    controller.Move(velocity * Time.deltaTime);
    }
    }


    mine says: Assets\Scripts\PlayerMovement.cs(23,22): error CS0176: Member 'Physics.CheckSphere(Vector3, float, int)' cannot be accessed with an instance reference; qualify it with a type name instead
     
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    8,727
    Maybe you should try
    Physics.CheckSphere
    instead?
    What is this for exactly?
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    7,881
    That's fine but please create your own threads and not hijack an existing (old) thread just because it has the same compilation error. These kinds of errors are from you typing stuff that isn't correct, it's not the SAME problem.

    Also, always use code-tags when posting code.

    Thanks.
     
    anycolourulike likes this.
  7. Saccotto15

    Saccotto15

    Joined:
    Jun 3, 2022
    Posts:
    1
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Dist : MonoBehaviour
    {
    public GameObject player;
    private float dist;
    public float maxDist;
    public Outline outline;

    // Start is called before the first frame update
    private void Start()
    {

    }

    // Update is called once per frame
    private void Update()
    {
    dist = Vector3.Distance(player.transform.position, transform.position);

    if (dist <= maxDist)
    {
    outline.OutlineMode = outline.OutlineMode.OutlineHidden;
    }
    }
    }

    Assets/Script/Dist.cs(25,35): error CS0176: Member 'Outline.Mode.OutlineHidden' cannot be accessed with an instance reference; qualify it with a type name instead
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    7,881
    Please do not hijack threads and try to read the Community Code of Conduct before proceeding. If you are going to hijack a thread then please read what the thread is about. This thread is marked as resolved and even describes what the problem is which is your understanding of Static members.

    Please create your own threads and always post code using code-tags and not plain text.
     
Thread Status:
Not open for further replies.
unityunity