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 Vencarii · Jun 22, 2016 at 05:30 PM · array of gameobjects

Strange results with FindGameObjectsWithTag()

Hello,

I have a strange problem with FindGameObjectsWithTag(), maybe someone can help.

I'm building a 2D game where the player can right click on the screen to open a context menu. This context menu has different buttons and when the user clicks a button, a GameObject is added to the level. When the player presses the space bar, the ball starts to move. When the space bar is pressed again, the added GameObjects should be saved and the level has to restart.

When I count the added GameObjects inside of the Update() method I get the correct result, but when I count them inside my SavePlacedObjects() method the number is always 2 too high and I have no idea why. (so when I added 1 object it's 3, when I added 2 objects it's 4 and so on)

EDIT: What's even more strange is the following behaviour:

When I place one object to the level and restart the scene I get "saving 3 objects".

But when I place one object to the level, pause the game, click on the placed object in my GameObject list and then restart the scene I get the correct value of "saving 1 objects".

I'm confused...

 void Update ()
 {
     // gives the correct value
     GameObject[] placedObjects = GameObject.FindGameObjectsWithTag ("Obstacle");
     print (placedObjects.Length + " placed");
 
     // open context menu
     if (Input.GetMouseButtonDown(1)) {
         OpenContextMenu ();
     }
 
     // close context menu
     if (Input.GetMouseButtonDown(0)) {
         if (EventSystem.current.IsPointerOverGameObject () == false) {
             CloseContextMenu ();
         }
     }
 
     // start the ball
     if (Input.GetKeyDown (KeyCode.Space)) {
             
         // restart the level when the ball rolls and space is pressed
         if (ballRigidbody.gravityScale > 0) {
             SavePlacedObjects ();
             SceneManager.LoadScene (SceneManager.GetActiveScene ().name);
         }
         else {
             ballRigidbody.gravityScale = ballGravity;
         }
     }
 }

And here the SavePlacedObjects() method:

 private void SavePlacedObjects ()
 {
     globalObject.placedObjects.Clear ();
 
     // result is always too high by 2
     GameObject[] placedObjects = GameObject.FindGameObjectsWithTag ("Obstacle");
     print ("saving " + placedObjects.Length + " objects");
 
     foreach (GameObject go in placedObjects) {
         globalObject.placedObjects.Add (go);
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

58 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

Related Questions

Im having trouble deleting a single game object from an array of game objects 0 Answers

BCE0019: 'gameObject' is not a member of 'Object'. 1 Answer

List wont appropriately add objects. 1 Answer

How to access Script B's Transform[] from my current script A? i going to drag scene object's transform into the new array script B. 0 Answers

Loot System that load items from resource folder to array 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