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

Object Reference required to access non-static member

Discussion in 'Scripting' started by Reymus, May 2, 2015.

  1. Reymus

    Reymus

    Joined:
    Apr 27, 2015
    Posts:
    44
    Hi there.

    I'm trying to implement Unity Ads, and have the script working fine to display the ads. Now I want to set up HandleShowResult and am getting errors.

    Here's my code:

    #if UNITY_IOS || UNITY_ANDROID
    private static void HandleShowResult (ShowResult result)
    {
    switch (result)
    {
    case ShowResult.Finished:
    Debug.Log("The ad was successfully shown.");

    gameController.AddGems(numberOfGems);
    gameOverPanel.SetActive (false);
    break;

    case ShowResult.Skipped:
    Debug.Log("The ad was skipped before reaching the end.");
    break;

    caseShowResult.Failed:
    Debug.LogError("The ad failed to be shown.");
    break;
    }
    }
    #endif

    I have a reference in Start() to access the GameController script that AddGems() is located in.

    I'm getting the following errors:

    Assets/Scripts/UnityAdsHelper.cs(167,25): error CS0120: An object reference is required to access non-static member `UnityAdsHelper.gameController'

    and

    Assets/Scripts/UnityAdsHelper.cs(168,25): error CS0120: An object reference is required to access non-static member `UnityAdsHelper.gameOverPanel'

    I'm at a loss.

    Thanks
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Where do you create instances of those objects? Can you post that code?

    That error means you're trying to use a function associated with a class as if it was static when it's not.

    This is static: Mathf.Clamp();

    This is not: gameObject.transform;
     
  3. pws-devs

    pws-devs

    Joined:
    Feb 2, 2015
    Posts:
    63
  4. Reymus

    Reymus

    Joined:
    Apr 27, 2015
    Posts:
    44

    Well, the AddGems() function is in my GameController script:

    public void AddGems (int newGemCount)
    {
    if (gameModeString == "NormalMode") {
    if (newGem) {

    gemCount += newGemCount;
    UpdateGems ();
    newGem = false;
    } else
    newGem = true;
    }
    }

    As for accessing my gameController, at the beginning of my UnityAdsHelper script, I have this:

    private GameController gameController;
    public int numberOfGems;

    And then I set up this:

    public void Start(){
    GameObjectgameControllerObject = GameObject.FindWithTag ("GameController");
    if (gameControllerObject != null)
    {
    gameController = gameControllerObject.GetComponent <GameController>();
    }
    if (gameController == null)
    {
    Debug.Log ("Cannot find 'GameController' script");
    }
    }