Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by yapadesouci · Sep 16, 2013 at 08:55 AM · gameobjectdebuggingmono

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yapadesouci · Sep 17, 2013 at 09:18 AM 0
Share

Thanks for your answer. I invoke my own initilize function.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges