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 /
This question was closed Aug 06, 2020 at 06:22 AM by Ilmu011 for the following reason:

Other

avatar image
0
Question by Ilmu011 · Aug 05, 2020 at 08:57 PM · scripting problemgameobject

Can't find gameobjects by name?

I have 13 gameObjects in my scene, which are named like this:

  • colored1

  • colored2

  • ...

  • colored13

When I run this script, all instances of the GameObject.Find() invocations return null:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MainScript : MonoBehaviour
 {
     // Start is called before the first frame update
     void Start()
     {
         for(int i = 1; i <= 13; i++)
         {
             string scope = "colored" + i;
             Debug.Log("Searching for " + scope);
             GameObject tile = GameObject.Find(scope);
             if(tile == null)
             {
                 Debug.Log(scope + " has been found");
             }
             else
             {
                 Debug.Log(scope + " could not be found");
             }
         }
     }
 }

However when I run THIS script, all gameobjects are found correctly (after I tagged them with "Tile"):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MainScript : MonoBehaviour
 {
     // Start is called before the first frame update
     void Start()
     {
         GameObject[] tiles = GameObject.FindGameObjectsWithTag("Tile");
 
         foreach(GameObject gameObject in tiles)
         {
             Debug.Log(gameObject.name);
         }
     }
 }


When I run that second script, all of the names of the gameObjects are also printed in the console correctly, colored1, colored2 and so on... why does the GameObject.Find() function not return any of them when I apparantly search for the correct names?

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 WarmedxMints · Aug 05, 2020 at 09:18 PM 0
Share

Looking at your code, it returns that it could not be found if it wasn't null and states that is has been found when it is null which is backward. If it is null, then it hasn't been found.

I'm not a finding by tag or name. I would have a script on the tiles which adds them to a list in their awake call. I tend to use scriptable objects for things like that.

1 Reply

  • Sort: 
avatar image
0

Answer by RealGoodGamerGuy · Aug 05, 2020 at 09:16 PM

Your if statement is checking if the tile is Equal to null. If it is, then the debug.log will say the object is found. Else it will say Object is not found. Swap the == for a != in the if statement.


Here's my solution. This version checks if the object is equal to something rather than null. If null then the object is not found. If equal to something then the object is found.

 // Start is called before the first frame update
     void Start()
     {
         for (int i = 1; i <= 13; i++)
         {
             string scope = "colored" + i;
             Debug.Log("Searching for " + scope);
 
             GameObject tile = GameObject.Find(scope);
 
             if (tile != null)
             {
                 Debug.Log(scope + " has been found");
             }
             else
             {
                 Debug.Log(scope + " could not be found");
             }
         }
     }






Comment
Add comment · Show 3 · 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 Hellium · Aug 05, 2020 at 09:34 PM 0
Share

This will have the EXACT outcome as OP's code

avatar image RealGoodGamerGuy Hellium · Aug 05, 2020 at 10:04 PM 0
Share

Original checks if the tile is Equal to null and saying that the object is found. Else the object is not found. So if the script actually finds the object in the scene it will say the object + " could not be found". BUT if the object is not found in the scene it will say the object + " has been found".

So I swapped the "==" for a "!=" to get the desired result.

avatar image Hellium RealGoodGamerGuy · Aug 05, 2020 at 10:16 PM 0
Share

$$anonymous$$y bad, I thought the OP wrote

          if(tile == null)
          {
              Debug.Log(scope + " could not be found");
          }
          else
          {
              Debug.Log(scope + " has been found");
          }

Follow this Question

Answers Answers and Comments

292 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 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 are these object passing through each other? 1 Answer

Calculating Scrolling GameObject x position scrolling pass another GameObject x postion (2D Game) 1 Answer

Update list on mouse click 1 Answer

How do I play gameobjects in my scene from one script / Script is not working 0 Answers

Assign spells/abilities dynamically to a GameObject? 0 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