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 /
  • Help Room /
avatar image
0
Question by Mythikos · Apr 08, 2016 at 06:43 AM · c#bugfunctionstart

Boolean set to true, false in Update() function because of Start()

I am having an issue where I set a boolean value in one function and is being overwritten by the time i need to use it in another function. For context, this is an NPC Core for a game I am developing. The player has the ability to set any of the pieces to start first (which subsequently can also be an NPC) and that is where my problem occurs. here are the scenarios:

Scenario 1 If a Player piece is selected to move first, everything works fine and the following pieces, including NPCs, all work properly.

Scenario 2 If an NPC piece is selected to go first, the NPC never moves because StartWait is still false for whatever reason.

Using Debug.Log and a little bit of debugging knowledge, I have tracked the issue down to being this. In scenario 2 from above, after I set the boolean, it appears Start() is running and setting the variable back to false. I know this because the output in the console says "Waffles Toaster" instead of "Toaster Waffles".

How could I go about correcting this so start does not override my previous assignment?

 // Initialization
 void Start()
 {
     Debug.Log("Toaster");
     NPCCoreVars.Timer = 0f;
     NPCCoreVars.StartWait = false;
     NPCCoreVars.ChosenPiece = null;
     NPCCoreVars.ChosenCell = null;
     NPCCoreVars.MoveOperation = 0;
     NPCCoreVars.CurrentNPCTeam = TeamChoices.None;
 }

 // Used to control the timer
 void Update()
 {
     // Are we waiting?
     if (NPCCoreVars.StartWait == false) return;

     // Decrement the timer
     NPCCoreVars.Timer -= Time.deltaTime;

     // Have we passed the timer?
     if (NPCCoreVars.Timer <= 0)
     {
         // Reset timer variables
         NPCCoreVars.StartWait = false;
         NPCCoreVars.Timer = 0;

         // Call the move function
         DoNPCMove();
     }
 }


 // The core function that decides what piece moves and where
 public void NPCMakeMove(TeamChoices NPCTeam)
 {
     ...
     if (NPCCoreVars.ChosenPiece != null && NPCCoreVars.ChosenCell != null)
     {
         NPCCoreVars.Timer = 2f;
         NPCCoreVars.StartWait = true;

         Debug.Log("Waffles");
     }
 }
Comment
Add comment · Show 1
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 Ali-hatem · Apr 08, 2016 at 10:24 AM 0
Share

the console says Toaster because Start() function is called only once it will not updated every frame.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Apr 08, 2016 at 10:45 AM

Well, it seems that you instantiate / enable your object and immediately call "NPCMakeMove" on it. That call will of course happen before Start is called since Start is delayed until the start of the next frame.

You should use "Awake" instead. Awake is called immedialtely when you enable the object. So if the prefab is already enabled, Awake will be called immediately when you Instantiate the object. If the prefab is disabled Awake will be called immediately when you enable it.

Apart from using Start or Awake you can always implement your own "Init" method and call it yourself at the right time.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to give instantiated object a value 0 Answers

unity doesnt work 0 Answers

Start function dont work 1 Answer

Problem with rotation to mouse position 0 Answers

Making a function that executes once change things gradually 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