Exception handling question

0 votes

I'm using unity and I'm trying to handle an exception.

I have CloudStuff class which is part of monobehaviour and I have UnityCallBack class in the same script.

I have another class on the same object, but in another script called MainMenu. I succesfully accessing values of MainMenu in CloudStuff class, but I can't access neither MainMenu class nor CloudStuff class from UnityCallBack class. I think it is just my poor knowledge of C#, but I'm not sure. Can anybody help me? So my goal is to access other classes from UnityCallBack.

When I print cloud object it is equal null, but I assign it new CloudStuff, maybe problem is here.

Here is the code.

using UnityEngine;
using UnityEngine.SocialPlatforms;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System;
using com.shephertz.app42.paas.sdk.csharp;
using com.shephertz.app42.paas.sdk.csharp.user;
using com.shephertz.app42.paas.sdk.csharp.game;
using com.shephertz.app42.paas.sdk.csharp.social;
using com.shephertz.app42.paas.sdk.csharp.abTest;
using com.shephertz.app42.paas.sdk.csharp.achievement;
using com.shephertz.app42.paas.sdk.csharp.avatar;
using com.shephertz.app42.paas.sdk.csharp.buddy;
using com.shephertz.app42.paas.sdk.csharp.connection;
using com.shephertz.app42.paas.sdk.csharp.customcode;
using com.shephertz.app42.paas.sdk.csharp.email;
using com.shephertz.app42.paas.sdk.csharp.geo;
using com.shephertz.app42.paas.sdk.csharp.log;
using com.shephertz.app42.paas.sdk.csharp.message;
using com.shephertz.app42.paas.sdk.csharp.pushNotification;
using com.shephertz.app42.paas.sdk.csharp.recommend;
using com.shephertz.app42.paas.sdk.csharp.review;
using com.shephertz.app42.paas.sdk.csharp.reward;
using com.shephertz.app42.paas.sdk.csharp.session;
using com.shephertz.app42.paas.sdk.csharp.shopping;
using com.shephertz.app42.paas.sdk.csharp.storage;
using com.shephertz.app42.paas.sdk.csharp.upload;
 
using AssemblyCSharp;
public class CloudStuff : MonoBehaviour 
{
 
ServiceAPI api;
public UserService userService; // Initializing User Service.
ScoreBoardService scoreBoardService;   
ScoreService scoreService;  
GameService gameService;   
public UnityCallBack callback;
public string gameName;
public MainMenu mainMenu;
 
 
#if UNITY_EDITOR
public static bool Validator (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
{return true;}
#endif
void Start ()
{
#if UNITY_EDITOR
ServicePointManager.ServerCertificateValidationCallback = Validator;
#endif
api =  new ServiceAPI("421bd5f87fdfd68a9d70d521014fa4cbad478502cab02c0862135d64e02738ba","78a2e8e9af3f6d5e00cfe9b11a876f702ddcbe6e3a6e2354ee6f2a60e03c90e6");
callback= new UnityCallBack();
userService = api.BuildUserService ();
scoreBoardService = api.BuildScoreBoardService();
scoreService = api.BuildScoreService(); 
gameService = api.BuildGameService();
gameName = "CarGame";  
 
mainMenu=this.gameObject.GetComponent<MainMenu>();
 
App42Log.SetDebug(true);        // to avoid printing Json into command line we will comment it 
 
 
gameService.GetGameByName(gameName, callback); 
 
}
 
public void CreateUser(string userName, string password, string email)
{
userService.CreateUser(userName,password,email,callback);
}
public void AuthentificateUser(string userName, string password)
{
userService.Authenticate(userName, password,callback);
}
public void setMenuFalse()
{
mainMenu.AuthentificationFailed=true;
}
 
 
 
}
public class UnityCallBack : App42CallBack  
{  
 
CloudStuff cloud=new CloudStuff();
 
public void OnSuccess(object response)  
{  
if(response is User)
{
User user=(User) response;
Debug.Log(user.GetUserName());
App42Log.Console("userName is " + user.GetUserName());  
App42Log.Console("emailId is " + user.GetEmail());   
}
 
}  
public void OnException(Exception e)  
{  
 
App42Exception exception = (App42Exception)e;  
int appErrorCode = exception.GetAppErrorCode();
if(appErrorCode  == 2005) 
{
Debug.Log("Email already exists.");
}
if(appErrorCode  == 2002) 
{
Debug.Log("Authentification Failed");
cloud.mainMenu.AuthentificationFailed=true;
 
}
App42Log.Console("Exception : " + e);  
}  
 
}  
 
asked Mar 27, 2014 in App42 Cloud API-BaaS by constantgenerator (10 points)
edited Mar 27, 2014 by constantgenerator

1 Answer

0 votes

I tested your script, its working fine.

You are accessing the other classes from UnityCallBack 

cloud.mainMenu.AuthentificationFailed=true;

here you are accessing the MainMenu class member AuthentificationFailed.

So, exactly what you want to access. 

 

 

answered Mar 28, 2014 by Akshay.Mishra (179 points)
Yes it was the thing I wanted to access, but the cloud object even after I assign new CloudStuff to it is null, so I cant access mainMenu of null. I fixed it using static functions and variables, but it will still be good to know why CloudStuff cloud = new CloudStuff(); returns null (empty object)
This is because creating a new instance of a class that derives from MonoBehavior is not allowed. Unity also throws a warning if you do so.

In your code the class CloudStuff is derived from MonoBehavior and hence the null reference.

You can also discussion on this issue on Unity forum
http://forum.unity3d.com/threads/119537-You-are-trying-to-create-a-MonoBehaviour-using-the-new-keyword
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...