user1831943
user1831943

Reputation: 21

How to pass a MonoBehaviour to a static method?

Sorry for bad title , I didn't know what exactly should call it. I have this Code :

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ScoreBridge : MonoBehaviour {

public static string resultText;

public static string GetScores()
{
    HighscoreSaver.loadScores(this);
    return resultText;
}
public void OnHighscoreLoaded(List<HighscoreSaver.Highscore> highscores)
{
    Debug.Log("Updating highscores!");
    string text = "";
    foreach (HighscoreSaver.Highscore hs in highscores)
    {
        text += hs.name + "\t\t" + hs.score + "\n";
    }
    resultText = text;
}

And I need to Run HighscoreSaver.loadScores(this) on GetScores() calls, but I can't use 'this' keyword in static methods. any advises are acceptable. thanks

Upvotes: 1

Views: 892

Answers (1)

Can Baycay
Can Baycay

Reputation: 875

There is no magic way of passing references into static contexts. So you have to pass your ScoreBridge instance to GetScores method, if you really want to make it static.

I think what you want is implementing a singleton design pattern. Take a look at here if you're not familiar with it.

http://wiki.unity3d.com/index.php/Singleton

Upvotes: 1

Related Questions