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 CoalCzar · Sep 25, 2013 at 04:49 AM · updatefunctionstartawake

Start, Awake, Update. Any other ways to call functions from an empty GameObject?

So I know that I can call functions that I write in my Start(), Awake(), Update(), and FixedUpdate() functions, but is there any other place or any other way to call custom functions on an empty GameObject?

Basically, there are a lot of functions that I want to happen just once, but that are inappropriate to call from Start() or Awake(). I'm currently using booleans to turn them on and off inside Update(), but this seems tedious and backwards. Is there a better way to call a particular function only once when I need it? Do I Instantiate() a prefab with another script attached with the function being called in its Start()? That seems wasteful. Advice?

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
2
Best Answer

Answer by TonyLi · Sep 25, 2013 at 01:13 PM

It depends on when you need to call them.

When the component is enabled or the game object is activated? Use OnEnable(). (Or OnDisable() when it's disabled/deactivated.)

On collision with another object? Use OnCollisionEnter() or OnTriggerEnter().

The whole list is on the MonoBehaviour reference: http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html

The order in which they're called is documented here: http://docs.unity3d.com/Documentation/Manual/ExecutionOrder.html

If none of those fit, then a good general solution is to use coroutines. To can start a coroutine in Start(). The coroutine can hang around until it's time to do something. Then it can do something and end.

Comment
Add comment · Show 4 · 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 CoalCzar · Sep 26, 2013 at 05:48 AM 0
Share

How would a coroutine "hang around" and then end? I understand they can yield return and yield return new WaitForSeconds(), but I must be missing something.

avatar image TonyLi · Sep 26, 2013 at 01:16 PM 0
Share

Say you want your component to disable itself 2 seconds after it becomes enabled.

 function OnEnable() {
     DisableAfter2Seconds();
 }
 
 function DisableAfter2Seconds() {
     yield WaitForSeconds(2);
     enabled = false;
 }

In the code above (warning: untested, and I'm switching $$anonymous$$dsets from C# so pardon any errors), when the component is enabled, the DisableAfter2Seconds() function will idle for 2 seconds, then disable the component, and then the function will exit.

Or say you want to wait until a condition is true, and then do something. In the example below, the coroutine waits until the game object is within 5 units of a target object and then plays an animation.

 function Start() {
     ExplodeWhenInRange();
 }
 
 function ExplodeWhenInRange() {
     var distance = Vector3.Distance(transform.position, target.position);
     while (distance > 5) {
         yield;
     }
     animation.Play("Explode");
 }
avatar image CoalCzar · Sep 26, 2013 at 03:51 PM 0
Share

Brilliant! Sometimes you just get stuck on an idea and you need someone to spell it out for you. Thanks!

avatar image TonyLi · Sep 26, 2013 at 05:34 PM 0
Share

Glad to help!

avatar image
1

Answer by Jamora · Sep 25, 2013 at 05:08 PM

Your question is quite vague, but my first idea would be to use events.

You subscribe to a particular event in each script that is interested in it, then fire that event at appropriate times.

This is only available in C# as far as I know, though you can emulate it with a construct like in this answer.

An alternative to events is SendMessage. It uses reflection so it's not a good idea to use it in Update or anything that runs frequently.

Comment
Add comment · 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 CoalCzar · Sep 26, 2013 at 05:49 AM 0
Share

Thanks for this. I'm looking into events now, though it's a little over my head. I'll see if they can help.

avatar image
0

Answer by dorpeleg · Sep 25, 2013 at 09:50 AM

Start(), Awake(), Upadte(), etc. are function (you don't write function within them).

They are functions that are called automatically at a certain order (decided by the "MonoBehavior").

Any other functions you create (you can create any and as much as you want), needs to be called manually.

To call functions from one gameobject to the other, use GetComponent.

The most basic answer is: yes, you have to call your functions from within one of the MonoBehaviors for them to happen.

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

18 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

Related Questions

If I have 2 different scripts both have Awake() on 2 different GameObjects, which Awake() starts first? 2 Answers

FindGameObjectWithTag wont find in Start() or Awake() but will in Update() 1 Answer

awake and start functions not working 2 Answers

Function that triggers something as soon as Object is "Set Active" 3 Answers

Start() not called upon instantation? 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