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 petaryakov · Aug 13, 2019 at 10:09 PM · updateontriggerentersynchronize

OnTriggerEnter() does not synchronize variables with Update()

Hello people,

I have a scene with 3 objects(rigid bodies) and 1 trigger. I have attached a script to an EventSystem that has an array of GameObjects and should do the following. The first object of the array can be moved and when it collides with the trigger object, it can't be moved anymore in contrast to the next object in the array. When the second object collides with the trigger and then we can move the next object and so on.

This is my script

  public class SystemMove : MonoBehaviour {
         public float moveSpeed = 5;
         public GameObject[] objects;
         int number = 0; 
         void Update()
        {        
                 //Debug.Log("Number in UPDATE is : " + number);  // It still prints 0 despite being incremented in OnTriggerEnter()
                 //Debug.Log("objects.length in UPDATE: " + objects.Length);   // Prints 3
                 objects[number].transform.Translate(moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, 0f,  moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime); 
         }
          private void OnTriggerEnter(Collider other)
         {
                if(other.name == "Trigger")
                {            
                        number++;
                        Debug.Log("objects.Length: " + objects.Length);   // Prints 0   WHY??????            
                        Debug.Log(" Number: " + number);    // Prints 1
                }
  } }

So why does OnTriggerEnter() prints 1 for the number variable and 0 for objects.lenght, while Update() prints 0 for number and 3 for objects.length.

Why are they not sychornized?

I don't know if EventSystem is the right object to attach this script. Maybe that is the problem, but the synchronization problem was really unexpected

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Aug 15, 2019 at 12:40 AM

You most likely attached this script to multiple objects at once. One script instance probably has it's object list initialized while the other one has not. We can not tell you more since we have no idea how your scene looks like, which objects are in the scene and which script is attached to which object(s).


Anyways you can pass a context object to your Debug.Log calls as second parameter. When you click on the log message in the console, Unity will highlight that context object so you know where it came from

 Debug.Log("OnTriggerEnter:: Number: " + number, gameObject);
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

181 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 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 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 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 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 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 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 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 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 avatar image

Related Questions

why OnTriggerEnter is not work if with void Update 2 Answers

How to move OnTriggerEnter to Update 1 Answer

check every frame if collider entered the another collider or not 1 Answer

How to allow only one object in a trigger at a time? 2 Answers

[SOLVED] OnTriggerEnter not working correctly. 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