i´m making all the code of constructor correct, but is not returning NOTHING. Follow my code, please give me a help about it.
using UnityEngine; using System.Collections; public class Novo : MonoBehaviour{ public int x1; public int x2; public int x3; public Novo () { Debug.Log (“Deu tudo certo”); } public Novo (int y1, int y2, int y3) { y1 = x1; y2 = x2; y3 = x3; } } public class Nova { Novo Lucas = new Novo (10,20,30);
public int Funcao (int caio, int Gisa) { int resultado = caio + Gisa; return resultado; }
next, the regular C# constructor should not be used when inheriting from MonoBehaviour. You use ‘Awake’ and ‘Start’ messages instead (just create a method named Awake and/or Start and they’ll be called automatically by unity).
This is to do with the way unity deals with serialization, and the constructor may run not on the main thread, or when anything should be accessed. This includes ‘Debug’.
Personally, I’ve found no problems with using constructors on MonoBehaviours, as long as they don’t rely on anything inside the UnityEngine and UnityEditor namespaces. Mostly, though, I only use them for setting defaults to properties, which aren’t serialized anyway.
@Dameon - I too use them sometimes as well for similar things. But I rarely suggest it to anyone, as you need to make sure what you’re accessing is ok to access, and it’s often just easier to tell people to just use ‘Awake’. Furthermore, I do so sparingly since Unity might go and change up something that could break it, since their documentation says to not do it all.
Note to OP, if you use the ISerializationCallbackReceiver interface ever:
Thanks a lot by the help, but i dont understand yet this concept (Sorry by my fail, but i’m a student =( ). I try insert the functions void Start () or Void Awake (), and remove the MonoBehaviour inheritance, but now this message, is showing in Debug.Alert = The class defined in the script file named ‘VectorGrid’ is not derived from MonoBehaviour or ScriptableObject. Please, anyone can post, all my script again, with the revisions. Again, thanks for all, by the help.