- Home /
Unity C# Start function without extending Mono
I was wondering, Is there a function what calls like the start function for classes which do not extend mono? I read that the " Main " function is used for C# outside of Unity but I was not sure what it could be in unity. If there is a function that exists that does this?
Answer by MikeNewall · May 25, 2013 at 06:49 PM
You can use a constructor. Create a method with the same name as your class:
public class MyAwesomeClass
{
public MyAwesomeClass()
{
//Any code here is called when you create a new instance of this class
}
}
You can pass it parameters too which you can use to initialize the object.
Use the "New" keyword to create a new instance of your class and the constructor does its stuff. You're using them every time you create a new Vector3!
Vector3 myVector = New Vector3(0, 10, 0);
You can use constructors with Static classes too:
http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.80).aspx
Answer by shavais · May 25, 2013 at 09:51 PM
As far as I know, the Unity engine (which contains the "main" function you mentioned) only calls certain functions of monobehaviours that have been assigned to objects that are setup (in the editor) to exist on startup. (Start, Awake, Update, FixedUpdate, OnGUI, etc.)
The only other way game code gets executed is if it is free standing code (outside of any class or function) in a script that somehow ends up getting executed; for example a javascript or boo script that contains a class that gets loaded.
The scripting overview talks about these things, and about ways to access code and variables in other scripts and such:
http://docs.unity3d.com/Documentation/ScriptReference/
One thing I've noticed that I don't think I saw spelled out in there very clearly (although it may just be me being thick-skulled) is that if you name a class the same as the file containing it, then that class becomes globally accessible; in C# it becomes part of the global namespace, and you can construct instances of it or access its static members from anywhere. This sort of auto linking/loading functionality doesn't seem to work for things placed in C# namespaces, which may be part of why the word has been that the use of namespaces for monobehaviours isn't supported yet (as of Unity 3.5.6, not sure about 4.0, but I don't think that's changed yet).
My impression is that people commonly set a particular scene as being the starting scene (by making it the 0'th scene in the build settings), then set a camera in that scene as being the main camera, and assign a (potentially very small) monobehaviour to that camera, and use the Start function of that monobehaviour to launch everything else. From there, as mentioned in other answers, you can construct your own classes, run functions of constructed instances, run static functions of your own classes, etc. I've been able to use C# namespaces just fine for non-monobehaviour classes as long as I set up access to them in the usual way (using, use, etc).
Your answer
Follow this Question
Related Questions
Need my function to work with different lists of different values (classes) 1 Answer
Question about classes [will specify once I know what the name of what I'm looking for is] 0 Answers
Instantiating objects from a class? (C#) 1 Answer
function isnt working in the class 0 Answers
How do I use a function in a class more than once? 4 Answers