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
2
Question by Syntax_Error752 · Jun 18, 2013 at 07:42 AM · gameobjectvariableif

How do I detect if a game object exist?

How do I detect if a game object exists in this format (Using a variable)?

 var WeaponViewPrefab : GameObject;
 
 if (GameObject.Find(WeaponViewPrefab) != null) {
     //Do something
 }
Comment
Add comment · Show 2
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 robertbu · Jun 19, 2013 at 06:55 AM 4
Share

The term "prefab" is missued. A true prefab will never be in the hierarchy, since it is only a template for a game object, not an actual game object:

http://docs.unity3d.com/Documentation/$$anonymous$$anual/Prefabs.html

If you are talking about game objects that either are in the scene at startup or you create through Instantiate(), you can use GameObject.Find() like you are trying to do here. The only issue is that it takes a string:

 if (GameObject.Find("WeaponViewPrefab") != null) {
     //Do something
 }



avatar image Syntax_Error752 · Jun 19, 2013 at 09:05 AM 0
Share

It does not work if I type it as a string, will I be able to use ".ToString()"?

So I need the script to check if the game object "WeaponViewPrefab" is already instantiated (Just to clarify).

4 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Syntax_Error752 · Jun 19, 2013 at 11:39 AM

Ok, I found it:

 var WeaponViewPrefab : GameObject;
 
 if (GameObject.Find(WeaponViewPrefab.name) != null) {
      //Do something
 }

Looks like I just answered my own question.

Comment
Add comment · Show 2 · 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 cregox · Dec 12, 2013 at 02:57 PM 0
Share

You do realize this is basically what @robertbu told you 4 hours earlier in the comments, right? Except he could have used WeaponViewPrefab.name (as you did) ins$$anonymous$$d of confusingly used the var name inside a string... In any case, you should accept this as the answer.

avatar image cregox · Dec 12, 2013 at 03:00 PM 0
Share

GameObject.Find won't search for disabled objects. For that you need to roll your own find.

avatar image
3

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

Look duplicate with this question : http://answers.unity3d.com/questions/26596/how-do-i-detect-if-a-game-object-exist.html 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 · 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 Pacosmico · Aug 08, 2018 at 07:39 PM 0
Share

I'm not sure if this wasn't the case in 2013 but it is now, this should be set as correct answer I think

avatar image
0

Answer by SubatomicHero · Jun 18, 2013 at 07:51 AM

It's very simple actually:

 // you just check that name of your game object
 
 if (WeaponViewPrefab) {
     // Do something
 }
 

Basically you're checking that it exists and the value holds true i.e. is not null

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 Syntax_Error752 · Jun 19, 2013 at 06:44 AM 0
Share

This checks if the variable has been defined, I need to check if the prefab exists (if it is in the hierarchy).

avatar image SubatomicHero · Jun 19, 2013 at 06:50 AM 0
Share

Do you mean that you want to check whether it has been instantiated correctly? How do you instantiate it at the moment?

avatar image SubatomicHero · Jun 19, 2013 at 06:53 AM 0
Share

Right I think you need to do this:

if (WeaponViewPrefab.activeInHierarchy) { // Do something }

This returns true if it is active in the scene.

avatar image
0

Answer by Griffo · Jun 19, 2013 at 09:41 AM

This is how I look through my hierarchy for any GameObject containing "Terrorist Type" in it's name, then I pass it onto another array I use just for those GameObjecs -

 private var gameObjects : GameObject[];
 private var closestTerrorist : GameObject[] = new GameObject[10];
 private var k : int;
 
 function Start(){
 
 // Search through all GameObjects in the hierarchy for ALL the Terrorists
     gameObjects = FindObjectsOfType(GameObject) as GameObject[];
     for (var i=0; i < gameObjects.length; i++){
         if(gameObjects[i].name.Contains("Terrorist Type")){
             closestTerrorist[k] = gameObjects[i];   // Pass the found Terrorists the the closestTerrorist[] array
             k++;
         }
     }
 }
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

19 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

Related Questions

if syntax for blank variable. 3 Answers

How do I assign a prefab reference to a private variable? 1 Answer

How to use if statement properly 1 Answer

Destroy(gameObject) does not work 2 Answers

Problems with the triggers(not recognized) 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