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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

An object reference is required to access a non static member

Discussion in 'Scripting' started by EliteWalrus, Jul 24, 2015.

  1. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    Code (CSharp):
    1.     public static void NewCurrentWeapon (string newWeapon) {
    2.         currentWeaponObject = PlayerScript.GetObjectFromArray (PlayerScript.weapons, newWeapon);
    3.         currentWeaponScript = currentWeaponObject.GetComponent <Weapon> ();
    4.     }
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You can't access non-static variables from a static method. Are currentWeaponObject and currentWeaponScript non-static?
     
  3. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    Both are static.

    Code (CSharp):
    1.     [HideInInspector]
    2.     public static GameObject currentWeaponObject;
    3.  
    4.     [HideInInspector]
    5.     public static Weapon currentWeaponScript;
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    And PlayerScript and GetObjectFromArray?
     
  5. EliteWalrus

    EliteWalrus

    Joined:
    Aug 16, 2014
    Posts:
    44
    GetObjectFromArray wasn't. Thanks man.