• 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
7
Question by CreativeCoding · Sep 03, 2010 at 11:38 PM · javascript

How do I detect if a game object exist?

In the scene?

btw, i'm using javascript.

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 ibx00 · Sep 02, 2018 at 10:04 AM 0
Share

Pretty much accurate just need to remember 1 extra thing about the name of the GameObject is that if it has been created in the hierarchy then the name should be the same but if that item has been instantiated form a prefab, the name will have "(Clone)" [with parenthesis] word as the suffix.

 if (GameObject.Find("WhateverItsCalled") != null)
 {
     //it exists
 }
 
 
 or
 
 if (GameObject.Find("WhateverItsCalled(Clone)") != null)
 {
     //it exists
 }

5 Replies

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

Answer by Mike 3 · Sep 04, 2010 at 12:03 AM

If you know the name of the object:

if (GameObject.Find("WhateverItsCalled") != null)
{
    //it exists
}
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 Jessy · Sep 04, 2010 at 12:03 AM 2
Share

!= null is not necessary.

avatar image Mike 3 · Sep 04, 2010 at 12:33 AM 3
Share

No, but it makes it more obvious (Plus I really can't stand the implicit version), and works in both js and c#, so bonus if it ever needs porting

avatar image adrianodigiovanni · Jul 13, 2017 at 09:26 AM 3
Share

Why traverse the hierarchy when you can

 using UnityEngine;
 
 public class $$anonymous$$y$$anonymous$$onoBehaviour : $$anonymous$$onoBehaviour
 {
     [SerializeField]
     private GameObject go;
 
     void Start()
     {
         if (go.scene.IsValid()) {
             // go is an instance of an object that's present in the scene
         } else {
             // go is an instance of a prefab
         }
     }
 }
 

I think hierarchy traversal should always be the last resource due to its performance implications.

avatar image
7

Answer by adrianodigiovanni · Jul 13, 2017 at 09:21 AM

 using UnityEngine;
 
 public class MyMonobehaviour : MonoBehaviour
 {
     [SerializeField]
     private GameObject go;
 
     void Start()
     {
         if (go.scene.IsValid()) {
             // go is an instance of an object that's present in the scene
         } else {
             // go is an instance of a prefab
         }
     }
 }
 
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 RockingGameDeveloper · Nov 15, 2020 at 04:23 AM 0
Share

He said javascript!

avatar image
3

Answer by dsh · Oct 26, 2014 at 07:07 PM

this works :)

         if (GameObject.Find ("Character(Clone)") != null) {
             Debug.Log ("get one");
         } else {
             Debug.Log ("not there");
         }

thx

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

Answer by ThinhHB · Oct 25, 2016 at 10:42 AM

Try this :

 var sceneName = testObject.scene.name;
 if (sceneName != null)
 {
     // this object is on scene
 }
 else
 {
     // this object is a prefab, not instantiate on any scene
 }

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

Answer by SuperRyn · Jan 16, 2019 at 06:00 PM

Im using this to check if the gameObject "pickleThing" is present like this:

if(GameObject.find("pickleThing") == null) { //insert code for showing text "mass extinction" here. }

Also give me the prefab version if this is incompatible with prefabs (also is this compatible with csharp?)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

10 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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

How to get started learning JavaScript or UnityScript? 6 Answers

JavaScript: ``Unknown Identifier`` 1 Answer

Collision Tags 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges