Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Zexx500 · Sep 23, 2013 at 11:58 PM · scenefunctionstartmain

Creating a scene using script (which Start() to use)

I want to create all the objects in the scene through scripting. That would be 1 camera, 1 light and 2 stretched quads with textures. The question is where do I put my code?

Is there an application-level Start() function? Or even better, a Start() function related to my particular scene. There are no other objects where to put my code, since the scene is empty at start.

Comment
Add comment · Show 1
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 clunk47 · Sep 24, 2013 at 02:37 AM 2
Share

You must have at the very least, one empty object in the scene with a single script attached to get things going. You could have some static classes in your assets folder and access them through this "main" script. Say you just have an empty object named "Scripts", and a script named "Startup". Then you have a script named "Static1" in your assets folder with some values. Why is that a probelm btw?

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Hoeloe · Sep 23, 2013 at 11:59 PM

You cannot execute code without having something existing in the scene. End of story. You could insert an empty game object with a single script to instantiate everything during Awake(), but unless you have something in the scene, nothing will be executed. There is nothing you can do about this.

Comment
Add comment · Show 18 · 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 Zexx500 · Sep 24, 2013 at 02:29 AM 1
Share

Ok, thanks for the answer. I hope that part of Unity will improve as it would be really useful for the scene to have scripts.

avatar image whydoidoit · Sep 24, 2013 at 06:29 AM 3
Share

Is it really so hard to have a single object in a single scene? I'm hoping they are concentrating on other features myself :)

avatar image Hoeloe · Sep 24, 2013 at 10:24 PM 1
Share

You could always instantiate a new script, or call other functions from the code, that would remove the huge case statement with a lot of code in it - at most you'd have one line in each statement, and it would be much more readable. Having said that, it does make a certain amount of sense as to why the scene itself doesn't allow this - Unity is designed to work with the editor - you're supposed to build environments with it, and while you can instantiate everything from script, it's not designed for that purpose. The overhead of a single dummy object in the scene is absolutely $$anonymous$$uscule - in fact, it's just about equal to the size of the GameObject class, which will be a few hundred bytes at most. If you only need the code to run at the start of the scene, you can always destroy that object at the end of the Start() function, too, which will stop it from having any impact at all on your scene - especially since destroying a single GameObject with only one script attached is a very $$anonymous$$or operation.

avatar image Hoeloe · Sep 25, 2013 at 10:04 AM 1
Share

As I said - you can destroy the GameObject immediately after the execution of the code, which removes it from the update loop. The benefits to having it on an object like this are in consistency, and the performance hit is barely larger than just executing the function on its own. You'd be surprised about how little scene initialisation is necessary, too, since games will actually often need to run things in the update loop without having a visible object there, too, so you might as well use that object for the initialisation. I think the problem you're having with this is that you consider a single GameObject to be a big overhead - it's really not. It's a few hundred extra bytes, for the duration of a single function, which are then discarded in 100ths of a second and never used again - it's simply not a huge overhead. You'd get a bigger overhead by setting up a regular expression, and many games use those left right and centre.

avatar image Hoeloe · Sep 25, 2013 at 10:36 AM 1
Share

But the scene itself isn't an object in the same way that GameObjects are. That's the point.

Show more comments
avatar image
1

Answer by Graham-Dunnett · Sep 24, 2013 at 10:56 AM

http://docs.unity3d.com/Documentation/ScriptReference/ScriptableObject.html

Comment
Add comment · Show 2 · 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 clunk47 · Sep 24, 2013 at 04:30 PM 0
Share

Good reference, just keep in $$anonymous$$d you still need a script in your scene to access these System.Object scripts. I know you know that, just posting for others that may not. +1 :)

avatar image Zexx500 · Sep 24, 2013 at 09:20 PM 1
Share

Thanks. Seems I'll have to use that.

avatar image
0

Answer by Martoonster · Feb 12, 2015 at 11:05 AM

I always create an empty object in the scene called God, and attach a script called God.cs. I use that script to manage all the top-level game stuff.

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

"Main" function? 3 Answers

How to call a function from a script in another scene 5 Answers

Is there a way to script the scene to pause itself or debug. 1 Answer

How to start a function from another scene. 1 Answer

run editor script when just returning from play mode 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