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 through variable from new Object (Newbie)

Discussion in 'Scripting' started by edutainment, May 19, 2016.

  1. edutainment

    edutainment

    Joined:
    Dec 19, 2014
    Posts:
    16
    I had followed Monobehavior said "You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed"
    Code (CSharp):
    1. obj data = new obj();
    changed to
    My Logic script (I wan to use monoBehaviour library too)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class custmHTTP :MonoBehaviour {
    5.     public float ans;     //My properite
    6.     void Start(){
    7.         ans = 2f + 2f;   //ans = 4 : inside here work perfectly
    8.     }
    9. }
    my new Object
    Code (CSharp):
    1.     custmHTTP data = gameObject.AddComponent<custmHTTP> ();
    2.         Debug.Log (data.ans);      //Get Zero or no assign anything from here
    3.  
    I got Null from my propertie(data.ans) then how to fix it or how to use correctly.
    Thank you .
     
    Last edited: May 19, 2016
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    There is no way you got 'null' from data.ans. Floats are never null. Unless you mean that 'data' was null, so you're getting a 'NullReferenceException'... is this what is happening?

    Also, work on your naming... 'ans', 'custmHTTP', 'obj'... you named a class 'obj'!? A common standard in mono/.net is that class names start with an uppercase letter, and use camel casing from there on out.
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    You're not getting "null", you're getting zero.

    That's because Start runs at the beginning of the next frame - which means that just after you've done an AddComponent, the value is still the default float value - 0.

    If you want something to happen in custmHTTP (don't name your classes like that) between the AddComponent and the Debug.Log, put it in Awake. Awake runs just as the object is created - think of it as the object's constructor if you've programmed in C# or a similar language before.
     
    lordofduct likes this.
  4. edutainment

    edutainment

    Joined:
    Dec 19, 2014
    Posts:
    16
    Sorry I got Zero sir ,Null from another logic I made you guys confusing. Anyway I want to make simple as c# and I want to "custmHTTP" happen like you guys said.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,181
    what?
     
  6. edutainment

    edutainment

    Joined:
    Dec 19, 2014
    Posts:
    16
    Actually here Im try to code
    Problame is I got Null from my propertie how to fix Baste

    Code (CSharp):
    1. public class custmHTTP :MonoBehaviour {
    2.     public JSONObject json;
    3.     public custmHTTP(){
    4.             StartCoroutine ("RequesWeb");
    5.     }
    6.      IEnumerator RequesWeb(){
    7.         WWW www = new WWW("http://localhost/cal/view_customerList.php");
    8.         yield return www;
    9.         if (!string.IsNullOrEmpty (www.error)) {
    10.             //error = www.error;
    11.         }else{
    12.             json = new JSONObject(www.text);  
    13.             Debug.Log (json.Count);        //Debug here Data work perfectly
    14.         }
    15.     }
    16. }
    here My calling
    Code (CSharp):
    1.     void Start () {
    2.         custmHTTP data = gameObject.AddComponent<custmHTTP> ();
    3.         Debug.Log (data.json.);    //get Null
    4.         //Debug.Log(data.json.Count)  
    5.     }
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    json isn't set until well after you've added the component.

    custmHTPP has to be created, 'Start'ed, run the Coroutine, which then has to wait for the WWW to complete.

    This does NOT happen all at once when you call 'AddComponent'. You have to wait until all of that is done.

    I am not sure how to exactly explain how to do all that for you due to our language barrier, and the fact it requires completely refactoring your code.
     
  8. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    As far as how to wait, delegates are a fairly straightforward way. You pass in a function that you want to be called when some future event happens (such as the data loading being complete), and then you can continue processing in that function.

    And as has been said, tighten up your naming conventions, it will make it easier for other people (and even yourself, a few months later) to read the code.

    Edit: Just noticed - if custmHTTP is a MonoBehaviour, you should be starting the coroutine in Start or Awake, not in the constructor.