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. Dismiss Notice

How to access MonoDevelop from other interface?

Discussion in 'Scripting' started by johnnydj, Jun 7, 2014.

  1. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    I have the below method which is inside a MonoDevelop class.
    This method inherits from App42CallBack and I need to access variable declared outside of this method.
    How could I access these public variables without making them static?

    Code (csharp):
    1.  
    2. public class FacebookConnection : MonoBehaviour
    3. {
    4. public string myVar;
    5.  
    6. public class UnityCallBackFriendsScore : App42CallBack
    7.   {
    8.   public void OnSuccess(object response)
    9.   {
    10.   Game game = (Game) response;
    11.  
    12.   Debug.Log("gameName is " + game.GetName());  
    13.   for(int i = 1;i <= game.GetScoreList().Count; i++)
    14.   {
    15.   Debug.Log("userName is : " + game.GetScoreList()[i].GetUserName());
    16.   Debug.Log("score value is : " + game.GetScoreList()[i].GetValue());
    17.   Debug.Log("scoreId is : " + game.GetScoreList()[i].GetScoreId());
    18.   Debug.Log("Created On : " + game.GetScoreList()[i].GetCreatedOn());
    19.   Debug.Log("Facebook id is : " + game.GetScoreList()[i].GetFacebookProfile().GetId());
    20.   Debug.Log("Facebook name is : " + game.GetScoreList()[i].GetFacebookProfile().GetName());
    21.   Debug.Log("Facebook picture is : " + game.GetScoreList()[i].GetFacebookProfile().GetPicture());
    22.   //access "myVar" variable here
    23.  
    24.   }
    25.   }
    26. }
    27. }
    28.  
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    What variables? Where are they?
     
  3. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    public string myVar;
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Something like;

    Code (csharp):
    1.  
    2. public class FacebookConnection : MonoBehaviour
    3. {
    4.   public string myVar;
    5.  
    6.   public class UnityCallBackFriendsScore : App42CallBack
    7.   {
    8.     private FacebookConnection connection;
    9.  
    10.     public UnityCallBackFriendsScore(FacebookConnection connection)
    11.     {
    12.       this.connection = connection;
    13.     }
    14.  
    15.     public void OnSuccess(object response)
    16.     {
    17.       Debug.Log(connection.myVar);
    18.     }
    19.   }
    20. }
    21.  
    Pass the instance of FacebookConnection when you create your UnityCallBackFrendsScore object.
     
  5. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    Thanks. that seems to be working for the variables.
    It almost works for StartCoroutine too.

    What I would also like to do is call a coroutine like: StartCoroutine("Player" + i);
    So I can start a coroutine based on the for loop iteration.
    Right now it works only by calling: connection.StartCoroutine(connection.Player1());
    This way I cannot control which coroutine to start by the for loop iteration.

    Any idea on this one?
     
  6. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Code (csharp):
    1.  
    2. if (i == 1)
    3.     connection.StartCoroutine(connection.Player1());
    4. else if (i == 2)
    5.     connection.StartCoroutine(connection.Player2());
    6.  
    Something like that? How method named "PlayerX" exists? Why would you need to number your method? Shouldn't it be;

    Code (csharp):
    1.  
    2. public void Player(int index) { }
    3.  
     
  7. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    It's almost working.
    The only problem I have now is that iterates through the for loop only once.
    As soon as it runs the "connection.StartCoroutine(connection.Player1());" it goes to the exception part with the error:
    Exception : com.shephertz.app42.paas.sdk.csharp.App42Exception: System.NullReferenceException: Object reference not set to an instance of an object

    Here is my code so far. In the first method I already had a new UnityCallBackFriendsScore()); so I had to pass in there the null otherwise it kept asking for the FacebookConnection.

    Code (csharp):
    1.  
    2. public void GetFacebookFriendsScore()
    3.   {
    4.   ScoreBoardService scoreBoard = new ScoreBoardService("123", "123");
    5.   scoreBoard.GetTopNRankersFromFacebook(levelName, FB.AccessToken, max, new UnityCallBackFriendsScore(null));
    6.   }
    7.  
    8.   public class UnityCallBackFriendsScore : App42CallBack
    9.   {
    10.   public FacebookConnection connection;
    11.   public UnityCallBackFriendsScore(FacebookConnection connection)
    12.   {
    13.   this.connection = connection;
    14.   }
    15.   public void OnSuccess(object response)
    16.   {
    17.   Game game = (Game) response;
    18.  
    19.   Debug.Log("gameName is " + game.GetName());  
    20.   for(int i = 0; i < game.GetScoreList().Count; i++)
    21.   {
    22.   Debug.Log("userName is : " + game.GetScoreList()[i].GetUserName());
    23.   Debug.Log("score value is : " + game.GetScoreList()[i].GetValue());
    24.   Debug.Log("scoreId is : " + game.GetScoreList()[i].GetScoreId());
    25.   Debug.Log("Created On : " + game.GetScoreList()[i].GetCreatedOn());
    26.   Debug.Log("Facebook id is : " + game.GetScoreList()[i].GetFacebookProfile().GetId());
    27.   Debug.Log("Facebook name is : " + game.GetScoreList()[i].GetFacebookProfile().GetName());
    28.   Debug.Log("Facebook picture is : " + game.GetScoreList()[i].GetFacebookProfile().GetPicture());
    29.   //access public variables here
    30.   if (i == 0)
    31.   connection.StartCoroutine(connection.Player1());
    32.   else if (i == 1)
    33.   connection.StartCoroutine(connection.Player2());
    34.   }
    35.   }
    36.  
    37.   public void OnException(Exception e)
    38.   {
    39.   Debug.Log("Exception : " + e);
    40.   }
    41.  
    42.   }
    43.  
     
  8. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Code (csharp):
    1. new UnityCallBackFriendsScore(null)
    Shouldn't that be

    Code (csharp):
    1. new UnityCallBackFriendsScore(this)
     
  9. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    oh dang :) that was the issue.
    Thanks for your help!