Faced the problem of how to post an account of the game in FB and Twitter. The script itself is and everything works, but only sends messages written to the text variable. The question is how to add a variable in which the game score is recorded. The variable is in another file, in another class. It turns out to access the method from that class. But the result of this access cannot be recorded and added anywhere.

This is a piece of code in which that variable lies:

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

Script for Twitter:

 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 somehow pull out the score variable from the GameOverScript class and add to this line (as I understand it) "?text=" + WWW.EscapeURL("Hurraahh! My Score in Test Game is: ")

  • Use the Sharetweet method to accept a string and pass the points where you need it from. - Monk
  • And at what point and where Sharetweet() call? - Alexey Shimansky
  • Can I have a little more detail on how to make it accept a string? - Rrttr
  • The script in which Sharetweet () is hung on the button. Clicking on twitter opens and requests confirmation of the publication - Rrttr
  • @Anstsiya is the most commonplace way to do it in the Sharetweet method Sharetweet var myScore = GameObject.Find("ИмяОбъктаСоСкриптомGameOverScript").GetComponent<GameOverScript>().score ... during the game, how and where is something recorded in the score ? and in PlayerPrefs.GetInt("Score") how and where? - Alexey Shimansky

2 answers 2

I will offer the following option:

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

Then, when publishing an account, you will need an instance of the GameOverScript class, which now has a score property that is readable. those. If we say a variable containing an instance of the GameOverScript class is called CurentGameOverRezult, then the publication will look like this:

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

If, say, in the code that publishes data about the current game, to get an instance of the GameOverScript class in which the data about the game account is stored which is not possible to publish, then you can make the following addition to the above code:

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

And then, accordingly, the publication will be:

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

In this case, the _score field and the score property are static, which, on the one hand, allows you to get the value of the score property without creating / receiving an instance of the class, and, on the other hand, imposes restrictions on the use of score. If you can have more than 1 instances of the GameOverScript class at some point in time, then in all these instances the score property will have the same value.

UPD: I read the question again and realized that in your case the following option would be optimal:

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

If you have

It turns out to access the method from that class.

In exactly the same way, you can access the score property of this class. Which will return the desired score of the game.

  • The error is the following: Error CS0120 An object reference is required for the non-static field, method, or property 'GameOverScript.score' - Rrttr
  • He swears that the GameOverScript.score field is not static. I'm not sure that for the last proposed option this modifier is available, but try changing the public int score .... to public static int score. In general, in this place: WWW.EscapeURL ("Hurraahh! My Score in Test Game is:" + GameOverScript.score) account data should be obtained in the same way as you got access to the method from this class. (I suppose this method was Start ()) - Alexey
  • Again, as suggested below, it is most likely possible to do this: WWW.EscapeURL ("Hurraahh! My Score in Test Game is:" + PlayerPrefs.GetInt ("Score"), but it’s impossible to say this for sure while the class hierarchy and modifiers methods used remain unknown to us ... - Alexey

And why can not do this: in the method Sharetweet

 score = PlayerPrefs.GetInt("Score"); 

Or get the instance of the GameOverScript class and call the getScore method on it to get the score.