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 Lr_K · Apr 26, 2011 at 09:18 PM · arraynullfindgameobjectswithtagarray-out-of-range-except

Find if no objects with tag exist

I have several waypoints with a tag "waypoint1" with govern the movement. However, I would like to check if any of them exist before attempting to move.

The problem I have is that it completely ignored the 'if null' check. How can I get that to work?

waypointNumber is a string.

This is my current code which doesn't work

void OnTriggerStay(Collider other) { if (other.tag == waypointNumber) { Debug.Log("hit"); if (other.transform.position == nextWaypoint) { Destroy(other.gameObject);

         if (GameObject.FindGameObjectsWithTag(waypointNumber) == null)
         {
             Debug.Log("null!");
             allowMove = false;
         }

         GameObject[] waypointArray = GameObject.FindGameObjectsWithTag(waypointNumber);
         nextWaypoint = waypointArray[1].transform.position;
     }
 }

}

I have tried using:

if (GameObject.FindGameObjectsWithTag(waypointNumber) != null{}else{DoStuff}

With no results.

I tried the waypointArray.Length == 0 like this:

if (other.transform.position == nextWaypoint) { Destroy(other.gameObject);

         GameObject[] waypointArray = GameObject.FindGameObjectsWithTag(waypointNumber);


         if (waypointArray.Length == 0)
         {
             Debug.Log("null!");
             allowMove = false;
         }

         nextWaypoint = waypointArray[1].transform.position;
     }

It ignored the "if (waypointArray.Length == 0)" and jumped straight to nextWaypoint = ... The console gave the error of "IndexOutOfRangeException: Array index is out of range."

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

Answer by DaveA · Apr 26, 2011 at 09:51 PM

Odd. I wonder if it returns an array of zero length? Could you try that? (Docs say null though)

Comment
Add comment · Show 4 · 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 Lr_K · Apr 27, 2011 at 08:50 AM 0
Share

Changing the central part to:

if (other.transform.position == nextWaypoint) { Destroy(other.gameObject);

GameObject[] waypointArray = GameObject.FindGameObjectsWithTag(waypointNumber);

  if (waypointArray.Length == 0)
  {
   Debug.Log("null!");
   allow$$anonymous$$ove = false;
 }

nextWaypoint = waypointArray[1].transform.position; }

Still doesn't fix the problem :/ It ignored the "if (waypointArray...)" and jumps straight to "nextWaypoint..". The debug console then says "IndexOutOfRangeException: Array index is out of range."

avatar image Lr_K · Apr 27, 2011 at 08:50 AM 0
Share

Ah, badly formatted. I'll edit the post to give the code details to make it more clear to read

avatar image DaveA · Apr 27, 2011 at 04:00 PM 0
Share

Sounds like it had 1 element. That would account for length != 0 and [1] being out of range (try [0])

avatar image Lr_K · May 03, 2011 at 09:33 AM 0
Share

This was the answer that lead me to the solution. The array was of 0 length but even so it took a while to figure out the problem. array[0] gave the current position because the waypoint wasn't actually destroyed at the time. So, destroying a waypoint flips a bool which then searches for new waypoints on the next update (by which point the old waypoint is fully destroyed).

If you need code clarification please send me a message.

Thank you for all the answers :)

avatar image
0

Answer by Unamine · Apr 27, 2011 at 10:18 AM

Try this: if (!GameObject.FindGameObjectsWithTag(waypointNumber))

The sign of negation (!) should be placed early in the condition as the example

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 Lr_K · Apr 27, 2011 at 10:25 AM 0
Share

I can't do that as "The !' operator cannot be applied to operand of type UnityEngine.GameObject[]'"

I guess this is because you can't check it on an array... Is there no way to check if an array is empty?

avatar image
0

Answer by Unamine · Apr 28, 2011 at 09:54 AM

So try to store GameObject.FindGameObjectsWithTag (waypointNumber) and put a variable like this:

var waypointsNumber // is an array if (! waypointsNumber) { // Place your code here }

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

No one has followed this question yet.

Related Questions

Instantiate specific object from "FindGameObjectsWithTag" Array 0 Answers

FindGameobjectsWithTag -> Array 2 Answers

Array error - Index is less than 0... 3 Answers

Null Reference Exception After refering to an Array 1 Answer

checking for nulls in arrays 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