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 laurienash · May 09, 2015 at 04:48 PM · javascriptcoroutinefunction

My function isn't ending but is being repeatedly being called from the update function

Hi,

I'm creating an animation sequence which I want to be set to active when a camera is set to active.

But I can't work out how to fix this - when I play this the update is calling my function continuously: what do I look into so that I only call the function once?

 #pragma strict

 var cameraPortacabin : GameObject;
 var text1 : GameObject;
 var text2 : GameObject;
 var text3 : GameObject;
 var screen1 : GameObject;
 var screen2 : GameObject;
 
 function Start () {
 
 text1.SetActive(false);
 text2.SetActive(false);
 text3.SetActive(false);
 screen1.SetActive(false);
 screen2.SetActive(false);
 
 }
 
 function Update () {
 
 if (cameraPortacabin.active == true) {
 
 portacabinSequence();
 
 
 
 }
 
 }
 
 
 function portacabinSequence () {
 
 yield WaitForSeconds (1.5);
 
 screen1.SetActive(true);
 
 yield WaitForSeconds (2);
 
 text1.SetActive(true);
 
 yield WaitForSeconds (5);
 
 text1.SetActive(false);
 text2.SetActive(true);
 
 yield WaitForSeconds (4);
 
 text2.SetActive(false);
 screen1.SetActive(false);
 screen2.SetActive(true);
 text3.SetActive(true);
 
 yield WaitForSeconds (3);
 
 text3.SetActive(false);
 screen2.SetActive(false);
 screen1.SetActive(true);
 
 yield;
 
 }



(Sorry for the newb question, I'm just getting so many errors thrown when I'm trying to fix this. I can't work out if I should be calling it once from the update function, or if I should stop the coroutine replaying from inside the function)

Best, Laurien

Comment
Add comment · Show 3
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 laurienash · May 09, 2015 at 05:48 PM 0
Share

Wow that works perfectly thank you (if you put your comment into an answer I can tick it as answered)

Why is it better to call the function directly from the script which enables the camera object?

avatar image ByteSheep · May 09, 2015 at 06:03 PM 0
Share

This code would work, but isn't as clean as it could be. Unfortunately UnityScript doesn't support events, which would be useful in a situation such as this, to notify other scripts that the camera object has been disabled.

There may also be some solutions that work with UnityScript, for example this article could be helpful in explaining a bit about events. Either way, there's lots of different approaches to solving the same problem :)

avatar image laurienash · May 09, 2015 at 10:05 PM 0
Share

Thanks - I'll have a read of that now :)

1 Reply

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

Answer by ByteSheep · May 09, 2015 at 05:35 PM

If I understand correctly, the problem is that the portacabinSequence function is being called multiple times when the camera object is set active.

Perhaps the easiest way of modifying your current code would be to add a variable that holds the last known state of the camera object, e.g.:

 // ... other variables etc
 var lastKnownState : Boolean = true; // set default to false if camera starts disabled
 
 function Update ()
 {
    if (cameraPortacabin.active != lastKnownState)
    {
       lastKnownState = cameraPortacabin.active;
       
       if (lastKnownState) // same as if (lastKnownState == true)
          portacabinSequence();
    }
 }

(Not tested, but something along those lines could work.) However, it would perhaps be better to call the portacabinSequence function directly from the script which enables the camera object. You could use SendMessage() or access the script through a reference to do this, e.g.

 MyAnimationScript.portacabinSequence();

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

Unityscript: Accessing functions from other scripts 1 Answer

Coroutine a function within a loop? 1 Answer

How to call a c-sharp function from javascript? 1 Answer

Update function doesn't work correctly 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 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