I had followed Monobehavior said "You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed"obj data = new obj();
changed to
My Logic script (I wan to use monoBehaviour library too)
using UnityEngine;
using System.Collections;
public class custmHTTP :MonoBehaviour {
public float ans; //My properite
void Start(){
ans = 2f + 2f; //ans = 4 : inside here work perfectly
}
}
my new Object
custmHTTP data = gameObject.AddComponent<custmHTTP> ();
Debug.Log (data.ans); //Get Zero or no assign anything from here
I got Null from my propertie(data.ans) then how to fix it or how to use correctly.
Thank you .
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.
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.
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.
Actually here Im try to code
Problame is I got Null from my propertie how to fix Baste
public class custmHTTP :MonoBehaviour {
public JSONObject json;
public custmHTTP(){
StartCoroutine ("RequesWeb");
}
IEnumerator RequesWeb(){
WWW www = new WWW("http://localhost/cal/view_customerList.php");
yield return www;
if (!string.IsNullOrEmpty (www.error)) {
//error = www.error;
}else{
json = new JSONObject(www.text);
Debug.Log (json.Count); //Debug here Data work perfectly
}
}
}
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.
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.