Load the results of the game on Unity on FB and Twitter



  • The problem was how to post the FB and Twitter account. The violin itself is and it works, but it only sends messages written in the original version. The question is how to add a variable in which the game is recorded. Reassigned to be in another file, another class. It comes to gain access to the method from that class. But the result of this access cannot be recorded and added.

    It's a piece of the code that's got that variable:

    public class GameOverScript : MonoBehaviour 
    {
        int score = 0;
    
    public void Start ()
    {
        score = PlayerPrefs.GetInt("Score");
    }
    

    ...
    }

    Twitter-a:

    public class Twitscript : MonoBehaviour
    {

    const string Address = "http://twitter.com/intent/tweet";
    public void Sharetweet()
    {
        Application.OpenURL(Address +
            "?text=" + WWW.EscapeURL("Hurraahh! My Score in Test Game is: ") +
            "&url=" + WWW.EscapeURL("\t") +
            "&related=" + WWW.EscapeURL("\t") +
            "&lang=" + WWW.EscapeURL("en"));
    }
    

    }

    We need to get the variable score out of GameOverScript and add it to this line. "?text=" + WWW.EscapeURL("Hurraahh! My Score in Test Game is: ")



  • I propose the following:

        int _score = 0;
        public int score
        {
            get { return _score; }
            private set { _score = value; }
        }
    
    public void Start()
    {
        score = PlayerPrefs.GetInt("Score");
    }
    

    Then you'll need a copy of the GameOverScript class that now has a score that is available for reading, i.e. if we say a variable containing a copy of GameOverScript class called CurentGameOverRezult, the publication will look as follows:

    Application.OpenURL(Address +
    "?text=" + WWW.EscapeURL("Hurraahh! My Score in Test Game is: "+CurentGameOverRezult.score ) +
    "&url=" + WWW.EscapeURL("\t") +
    "&related=" + WWW.EscapeURL("\t") +
    "&lang=" + WWW.EscapeURL("en"));

    If, for example, the code that publishes the current game is to obtain a copy of GameOverScript, which stores data on the score that cannot be published, the following may be added to the code given above:

    static int _score = 0;
    public static int score
    {
    get { return _score; }
    private set { _score = value; }
    }

    And then the publication will have the type:

     Application.OpenURL(Address +
    "?text=" + WWW.EscapeURL("Hurraahh! My Score in Test Game is: "+GameOverScript.score ) +
    "&url=" + WWW.EscapeURL("\t") +
    "&related=" + WWW.EscapeURL("\t") +
    "&lang=" + WWW.EscapeURL("en"));

    In this case, the field of _score and the characteristics of the score have become static, which, on the one hand, makes it possible to obtain the value of the score without creating/ receiving a copy of the class, and on the other, imposes restrictions on the use of the score. If the GameOverScript versions of you can have more than one at some point in time, all these copies will have the same value.

    UPD:
    Read the question again and realized that in your case the best option would be:

    public int score
    {
    get { return PlayerPrefs.GetInt("Score"); }
    }

    If you have

    Turns out to have access to the method from that class.

    The same way you can access this class score. Which will return the game you're looking for.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2