• 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
Question by Bunnybomb7670 · May 25, 2013 at 06:01 PM · c#functionclassclasses

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?

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

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

Comment
Bunnybomb7670
Flickayy

People who like this

2 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 Bunnybomb7670 · May 25, 2013 at 07:04 PM 0
Share

Oh i see, thanks for that!

avatar image

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).

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

class wont show up in inspector 1 Answer

Make inspector show changes of class variable value in function? 1 Answer

How can I access a function without knowing the script/class name to which it belongs? 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