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 Zaffer · Jun 19, 2012 at 01:48 AM · instantiateupdate

how to call something just once from an update function

Hi,

I have level0 (small) and level1 (large). I have the build set up so level0 loads first. I would then like to load an object (a door) into level0 when level1 had finished loading. I am using the Update() function to determine when level 1 has finished loading, but I find that if I try to instantiate the door from inside the Update() function, I get many many doors. Is there a way to get just one door? Here's the code. Thanks. Zaffer

 function Update() { 
 if(Application.GetStreamProgressForLevel(levelToLoad) == 1) { 
    Progress.message = "Level at index 1 has been fully streamed!";
    Instantiate(door, Vector3(154, 1.8, 166), Quaternion.identity);
    level1Ready = true; 
 }
 else {
     percentageLoaded = Application.GetStreamProgressForLevel(levelToLoad) * 100;
     Progress.message = percentageLoaded.ToString();
     level1Ready = false;
 }

}

Comment
Add comment · Show 6
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 Zaffer · Jun 19, 2012 at 02:42 AM 0
Share

Thanks Eric5h5,

Would greatly appreciate it if you would show me how to set up the coroutine. Thanks.

Zaffer

avatar image Eric5h5 · Jun 19, 2012 at 02:54 AM 0
Share

Have you read the docs about StartCoroutine and so on?

avatar image Zaffer · Jun 19, 2012 at 03:02 AM 0
Share

Thanks for the tip. I will read up on coroutines and try to figure out.

Zaffer

avatar image Zaffer · Jun 19, 2012 at 01:37 PM 0
Share

Hi Tim and Eric5h5:

Still getting lots of doors. Am I getting warmer? Thanks. Zaffer

function Update() { if(Application.GetStreamProgressForLevel(levelToLoad) == 1) { Progress.message = "Level at index 1 has been fully streamed!"; StartCoroutine($$anonymous$$akeDoor()); level1Ready = true; } else { percentageLoaded = Application.GetStreamProgressForLevel(levelToLoad) * 100; Progress.message = percentageLoaded.ToString(); level1Ready = false; } }

function $$anonymous$$akeDoor(){ yield Instantiate(door, Vector3(154, 1.8, 166), Quaternion.identity);

}

avatar image Tim-Michels · Jun 19, 2012 at 02:29 PM 0
Share

I think you don't need the Update() function.

I'll write something down here which I think is more like what your coroutine should look like: (sorry it's in C#)

IEnumerator LoadInOrder() { level1Ready = false;

     //Wait while the scene is loading, this while loop will act as a "temporary update" until the scene is fully loaded
     while (Application.GetStreamProgressForLevel(levelToLoad) < 1)
     {
         percentageLoaded = Application.GetStreamProgressForLevel(levelToLoad) * 100;
         Progress.message = percentageLoaded.ToString();
         yield return new WaitForEndOfFrame();
     }

     //Scene was fully loaded
     level1Ready = true;
     Progress.message = "Level at index 1 has been fully streamed!";
     Instantiate(door, Vector3(154, 1.8, 166), Quaternion.identity);
 }

You should start this coroutine after you started loading the level. Then, this coroutine will perform the while loop until it is fully loaded, whereafter your door will only be instantiated once.

Hope this is what you want to achieve.

Show more comments

5 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Eric5h5 · Jun 19, 2012 at 01:52 AM

You'd be better off not using Update, but rather a coroutine, since the point of Update is that it runs every frame. With a coroutine you can just wait until the event is done and run the code afterward once.

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
1

Answer by Tim-Michels · Jun 19, 2012 at 08:27 AM

Here's an exampe on how to use a coroutine: In this (really simple) example the coroutine gets started in the Start() function.

 void Start()
 {
     StartCoroutine(DoSomeStuff());
 }
 
 IEnumerator DoSomeStuff()
 {
 
     //"Ready" gets logged
     Debug.Log("Ready");
     
     //Coroutine will wait 1 second to continue
     yield return new WaitForSeconds(1.0f);
     
     //"Set" gets logged
     Debug.Log("Set");
     
     //Coroutine will wait 1 second to continue         
     yield return new WaitForSeconds(1.0f);
 
     //"Go" gets logged
     Debug.log("Go");
 
     ...
 }

You can do anything you like within a coroutine, the "yield" statement simply sets the function on "hold" for how long you choose.

You can also use "yield return new WaitForEndOfFrame()" and other functions. When you get the hang of coroutines, you can do some really cool stuff with it.

Cheers.

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
1
Wiki

Answer by Zaffer · Jun 19, 2012 at 02:30 PM

Hi,

It took me awhile, but I finally figured out I could do what I wanted with a simple Boolean variable, doorPresent, which I set false in the Start() function. Then I set that to true after making only one door. Seems to be working so far. Coroutines and yield look really interesting -- will study them more. Thanks. Zaffer

 function Update() { 
     if(Application.GetStreamProgressForLevel(levelToLoad) == 1) { 
        Progress.message = "Level at index 1 has been fully streamed!";
        //StartCoroutine(MakeDoor()); 
        MakeDoor();
        level1Ready = true; 
     }
     else {
         percentageLoaded = Application.GetStreamProgressForLevel(levelToLoad) * 100;
         Progress.message = percentageLoaded.ToString();
         level1Ready = false;
     }
 } 
 
 function MakeDoor(){
     //yield;
     if(doorPresent == false){
         Instantiate(door, Vector3(154, 1.8, 166), Quaternion.identity);
         doorPresent = true;
     }
    
 }
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 Tim-Michels · Jun 19, 2012 at 02:33 PM 0
Share

Well, this is merely a hack. It will work, but you're better off using the method I just posted.

Greets

avatar image Enigmaticall · Jul 25, 2014 at 07:33 PM 0
Share

This is how I handle everything haha, coroutines scare me! Thanks for the question though. Do you know if this method slows down the game at all or if it is okay?

avatar image
0

Answer by Zaffer · Jul 03, 2012 at 08:24 PM

Hi Eric5h5 and Tim,

I wanted to take your advice about coroutines being better, and after some work, I was able to set up a loading sequence using a while loop, yield and coroutines instead Update(). I needed a little help from regular forum, but everything works. Here's the code. Zaffer

P.S. You are right, Tim, couroutines are very cool!

function Start(){ ManageLoadingElements(); }

 function ManageLoadingElements(){
     yield StartCoroutine("SetUpLoadingElements");
     TakeDownLoadingElements();
 }
  
 function SetUpLoadingElements(){
     while(Application.GetStreamProgressForLevel(levelToLoad) < 1){
         percentageLoaded = Application.GetStreamProgressForLevel(levelToLoad) * 100;
         progressCylinder.transform.localScale.x = percentageLoaded * 0.1;
         progressCylinder.transform.position = progressCylinderPosition;
         progressText3D.text = Mathf.RoundToInt(percentageLoaded) + "%"; 
         yield;
     }
 }
  
 function TakeDownLoadingElements(){
     Destroy(progressCylinder);
     Destroy(progressText3D);  
     MakeDoor();
 }
  
 function MakeDoor(){
     var door : GameObject;
     if(doorPresent == false){
         door = Instantiate(doorPrefab, Vector3(157, 2.65, 169.59), Quaternion.identity);
         doorPresent = true;
         door.name = "Iron Door";
     }
 }


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
0

Answer by Luci85 · Jul 03, 2012 at 08:34 PM

I would use a boolean variable, that is true only for the first run.

 var firstCall:Boolean;
 void Start()
 {
     firstCall = true;
 }
 function Update() { 
     if(Application.GetStreamProgressForLevel(levelToLoad) == 1) { 
        Progress.message = "Level at index 1 has been fully streamed!";
        //StartCoroutine(MakeDoor());
        if(firstCall)
        {
           MakeDoor();
           firstCall = false;
        }
        level1Ready = true; 
     }
     else {
         percentageLoaded = Application.GetStreamProgressForLevel(levelToLoad) * 100;
         Progress.message = percentageLoaded.ToString();
         level1Ready = false;
     }
 }
Comment
Add comment · Show 3 · 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 Zaffer · Jul 03, 2012 at 09:48 PM 0
Share

That's a good strategy too. Thanks.

avatar image Luci85 · Jul 03, 2012 at 09:51 PM 0
Share

But if you get more than one door, then that means the else branch isn't called as it should be, right?

avatar image Zaffer · Jul 03, 2012 at 11:33 PM 0
Share

I have not actually tried your idea, just thought it looked like a good solution.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How should i add raycast to multiple game objects??? 0 Answers

Prefabs Transforms LookAt 0 Answers

Refrencing Instantiated Object 1 Answer

How to prevent object (bullet) from instantiating twice ? (It instantiates once for tapping the screen, and once more when beginning to hold touch on screen) ? 1 Answer

How to detect empty slot and spawn gameobject there? 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