Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Stardog · May 31, 2011 at 09:36 PM · updateprint

Simple "do once" in Update issue.

I want the "print" message to display once. The problem is that it's in the Update function so it prints to infinity as long as it's true.

 using UnityEngine;
 using System.Collections;
 
 public class Quest1Door : MonoBehaviour {
 
     void Update()
     {
         if (Quest1.openedDoor) {    
             print("The door is now open.");
         }
     }
 
 }

I tried putting break/return after it, but it's not a loop?

I also did a doOnce variable that was set to true, but it seems like a lot of extra code for something so simple. Then again, this is how Fallout 3/Oblivion scripts are done.

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

Answer by Eric5h5 · May 31, 2011 at 09:59 PM

Don't use Update, which always runs every frame regardless, and is very poor for any kind of event scheduling. Just call the function when you need to.

 public class Quest : MonoBehaviour {
 
     void PrintMessage (string message)
     {
         print message;
     }
 
 }

Get a reference to the Quest script, and when the player opens the door, do

 questScript.PrintMessage ("The door is now open.");
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 Stardog · May 31, 2011 at 10:37 PM 0
Share

I've changed it all into functions now. It's a little less easy to read because I have to get the object with GameObject.Find etc, but I guess it's the best way.

avatar image Eric5h5 · May 31, 2011 at 11:48 PM 0
Share

@Stardog: It should be easier to read if the code is structured properly. Also you might want to look into singletons: http://www.unifycommunity.com/wiki/index.php?title=A$$anonymous$$anagerClass

avatar image
2

Answer by ronronmx · May 31, 2011 at 09:50 PM

You need to use a boolean flag that you turn on and off when needed:

 using UnityEngine;
 using System.Collections;
 
 public class Quest1Door : MonoBehaviour {

     private bool printDone;

     void Start() 
     {
         printDone = false;
     }
 
     void Update()
     {
         if (Quest1.openedDoor && !printDone) {    
             print("The door is now open.");
             printDone = true;
         }
     }
 
 }

By setting "printDone" to true in the IF statement, your print() text won't be displayed again until "printDone" is set back to false (for example, the next time you restart the scene).

Hope this helps, I couldn't test the code but it should work. Stephan

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 Stardog · May 31, 2011 at 10:36 PM 0
Share

That's what I did with the doOnce variable and it works.

avatar image
1

Answer by Stardog · Jun 21, 2011 at 01:02 PM

Another answer for this - use this Messenger script:

http://www.levelbylevel.com/tutorials/unity-c-messenger-tutorial

http://www.unifycommunity.com/wiki/index.php?title=CSharpMessenger_Extended

Make the door send send a message:

 Messenger.Broadcast("door_opened");

Then, in the QuestScript, listen for it and change the variable with a function.

 void OnEnable() {
     Messenger.AddListener("door_opened", FunctionThatChangesVariable);
 }
 void OnDisable() {
     Messenger.RemoveListener("door_opened", FunctionThatChangesVariable);
 }
 
 public void FunctionThatChangesVariable() {
     print("The door is now open.");
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

OnTriggerEnter only recognised once, no matter how many times I enter ? (Solved) 1 Answer

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

Print statement to execute multiple times 2 Answers

Finding Closest Enemy... 2 Answers

How to print user's guide 3 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