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
1
Question by John A C · Jul 27, 2010 at 09:30 PM · javascriptfunctionscriptingbasicsoutside

running code outside of update!

Trying to run some basic code to control my game application. According to unity scripting reference all code has to be run inside and Update function. Meaning it will run every frame -and it has to be attached to an object!!

How do I get some code just to run once? Outside of any function when my game starts. Surely everyone needs a script like this somewhere in their project?

I really need to know where to put it. Putting the code outside of an update function just results in lots of errors.

Example:

print ("Game has loaded");

//call another script to start playing music for the menu screen var externalScript = gameObject.GetComponent(musicControl); externalScript.startPlayingIntroMusic();

//declare global variable lives = 10; var lives = 10;

Really want to do some good stuff in unity, but it's such a slow process. Every time I start I spend hours just trying to get the most simplest of scripts to work. Then it gets late and I have to leave it again!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Mike 3 · Jul 27, 2010 at 09:32 PM

Use the Start or Awake functions

Also - there are lots of other automatically called functions, not just Update - I'm not sure where you got that from

You can see the majority of the Overridable Functions section here:

http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html

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

Answer by fherbst · Jul 27, 2010 at 09:33 PM

You can use the Start() or Awake() functions. And put that script on some GameObject in your scene. Most people use something like a Master gameObject which handles all the game logic and so forth (as far as I know).

So, use something like this:

function Start()
{
  // Do-A-Lot-Of-Stuff
  // ...
  print("Game started.");
}
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 John A C · Jul 27, 2010 at 09:50 PM 0
Share

Thanks. That's useful and good to know. You think this would be where most examples or tutorials start but they dive straight in to character animation and physics ins$$anonymous$$d! Am sure I tried Start() and it didn't work but maybe the result of another error. I checked the hello world example in unity scripting reference thinking that would be a function that ran once -The example attaches it to an update so it loops!

Looks like most of my errors came from not understanding the term STATIC. Static functions are what I need, though I did not know what I was looking for.

avatar image John A C · Jul 27, 2010 at 09:54 PM 0
Share

turns out I also wrote start() ins$$anonymous$$d of Start() !!

avatar image
-1

Answer by Deeweext · Jun 11, 2013 at 11:38 AM

You read a tutorial before asking questions. I'm sure there is no where it says all code has to run inside an update function.

~ Old question bumped; Valid answer posted.

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

1 Person is following this question.

avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Inverse function of OnTriggerStay? 3 Answers

Script to teleport player (Beginner here) 0 Answers

Static function and variables error 2 Answers


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