- Home /
GameObject AddComponent and MonoBehaviour problem
Hi all,
I try to do a SDK for our service and I have a problem with MonoBehaviour which prohibit new operand. I understand that I must use AddComponent method on a GameObject instance.
So I do something like this:
I have a TestGameRocket.cs class containing:
public class TestGameRocket : MonoBehaviour {
// Use this for initialization
void Start () {
GameObject go = new GameObject("GameRocket");
go.AddComponent<GameRocketGateway>();
go.AddComponent<GameRocketService>();
GameRocketGateway gateway = GameObject.FindObjectOfType(typeof(GameRocketGateway)) as GameRocketGateway;
gateway.Environment = GameRocket.Environment.DEVELOPMENT;
gateway.Service = GameObject.FindObjectOfType(typeof(GameRocketService)) as GameRocketService;
gateway.Game.Find("5471f4a877514171bed4cea25b1a0af9");
}
// Update is called once per frame
void Update () {
}
}
The GameRocketGateway class is:
namespace GameRocket
{
public class GameRocketGateway : MonoBehaviour
{
public GameGateway game;
public GameRocketService Service { get; set; }
public Configuration Configuration { get; set; }
public Environment Environment
{
get { return Configuration.Environment; }
set { Configuration.Environment = value; }
}
public String Apikey
{
get { return Configuration.Apikey; }
set { Configuration.Apikey = value; }
}
public String Secretkey
{
get { return Configuration.Secretkey; }
set { Configuration.Secretkey = value; }
}
public GameRocketGateway()
{
Configuration = new Configuration();
}
public GameRocketGateway(Environment environment, String apikey, String secretkey)
{
Configuration = new Configuration(environment, apikey, secretkey);
}
public void Awake()
{
game = new GameGateway(Service);
}
public virtual GameGateway Game
{
get { return game; }
}
}
}
The GameRocketService.cs makes API Call with WWW and extends also MonoBehaviour for StartCoroutine method:
namespace GameRocket
{
public class GameRocketService : MonoBehaviour
{
public String ApiVersion = "1";
public Configuration Configuration;
public Environment Environment
{
get { return Configuration.Environment; }
}
public String Apikey
{
get { return Configuration.Apikey; }
}
public String Secretkey
{
get { return Configuration.Secretkey; }
}
public String BaseUrl
{
get { return Configuration.Environment.GatewayURL + "/" + ApiVersion; }
}
public GameRocketService(Configuration configuration)
{
Configuration = configuration;
}
public void Get(String url, Request request, System.Action<String> onComplete)
{
IDictionary<String, String> parameters = request.getParameters();
String signature = Crypto.Sign("GET", BaseUrl + url, parameters, Secretkey);
parameters.Add("signature", signature);
String qs = new QueryString().Append(parameters).ToString();
StartCoroutine(DoQuery(BaseUrl + url + "?" + qs, onComplete));
}
public IEnumerator DoQuery(String url, System.Action<String> onComplete)
{
var xhr = new WWW(url);
yield return xhr;
// check for errors
if (string.IsNullOrEmpty (xhr.error)) onComplete(xhr.text);
}
}
}
And the GameGateway.cs file containing:
namespace GameRocket
{
public class GameGateway
{
private GameRocketService Service;
protected internal GameGateway(GameRocketService service)
{
Service = service;
}
public virtual void Find(String id)
{
if (id == null || id.Trim().Equals(""))
throw new NotFoundException();
Service.Get("/games/" + id, new EmptyRequest(), result => {
Debug.Log(result);
});
}
}
}
The problem is that GameRocketService isn't injected and stay at null which throw a NullPointerException on:
Service.Get("/games/" + id, new EmptyRequest(), result => {
Debug.Log(result);
});
Can you help me please? Thanks in advance for your help.
Answer by Bunny83 · Sep 16, 2013 at 09:13 AM
You call this line in Awake:
game = new GameGateway(Service);
Awake is called before AddComponent returns. You should use Start instead or invoke your own initialize function.
Thanks for your answer. I invoke my own initilize function.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Making script only effect one gameobject when attached to multiple 2 Answers
Rotation help! 1 Answer
GUI controlling other game objects 0 Answers
Destroy GameObject A or B 1 Answer