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 schwin42 · Feb 21, 2013 at 01:39 AM · logicorderprioritysimultaneous

How does priority/ simultaneity work in Unity?

In the project I'm working on, I have objects that can destroy each other. The trouble is, they can't destroy each other at the same time--one always takes priority. Here is a simple example that will show the problem: I have two objects GameObject1 and GameObject2 with the following C# script attached:

 public class Simultaneous : MonoBehaviour {
     
     public bool isOn = true;
     public GameObject otherObject;
 
     void Update () {
     if(isOn){
         otherObject.GetComponent<Simultaneous>().isOn = false;
         }
     }
 }

Here, GameObject1 is assigned as GameObject2's otherObject and vice versa. When I run this program, what I expect to happen is for both scripts to update simultaneously and set the other object's isOn to false (i.e., both variables start true and become false). However, what does happen is GameObject1 sets GameObject2's isOn to false and GameObject1's isOn remains true. GameObject1 appears to have priority over GameObject2 even though they're functionally identical.

Why is this? Is there any way to have both objects disable each other simultaneously?

Thanks for your help.

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

2 Replies

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

Answer by Dave-Carlile · Feb 21, 2013 at 02:07 AM

The code executes sequentially in Unity, always. The Update functions are called by Unity in a loop, and each executes to completion before the next starts.

The only way to have code execute simultaneously is via threads, and even then it only executes simultaneously if you have multiple processors or cores. And, you can't execute Unity API functions via any thread other than the main application thread.

Unity does offer a form of parallelism by using Coroutines. However these still execute sequentially.

So, once your Update function executes on one of the objects, it disables the other. When the other Update function runs, it does nothing because it's already disabled.

If you want them both changed at the same time, try using a third script that controls them both.

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 schwin42 · Feb 21, 2013 at 04:22 AM 0
Share

This explanation helps immensely, thanks for posting.

avatar image
1

Answer by Owen-Reynolds · Feb 21, 2013 at 02:18 AM

General computer rules are that nothing happens "at the same time," and if you try, the order will be arbitrary. For example, the swap: a=b; b=a; turns them both into b, since they run one at a time.

Many problems like yours can be solved by checking state changes -- check only at the moments when something switches to ON or OFF. For example, "turn on Obj 1" would be: if ob2 is ON, turn off Obj2 (and leave Obj1 off -- we are both off); else turn on Obj1.

The "gain pain but always works" solution is to have a master script do the work. Make an "old" copy of everyone's ON/OFF. Then use the old values to update the current. Then the order won't matter, since the "old" copies stay the same.

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 schwin42 · Feb 21, 2013 at 04:21 AM 0
Share

This clears things up a lot, thank you!

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

11 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

Related Questions

Camera Auto-Align with the nearest object with a determinated tag 0 Answers

Passing a string from Javascript to C# 1 Answer

Order of Update(), Awake(), etc of gameobjects and their child gameobjects 1 Answer

[Solved]How to destroy a turret in Tower Defense game 2 Answers

Physic Material Friction Combine Mode lack of documentation? 2 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