- Home /
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
}
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
}
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).
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.
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.
GameObject.Find
won't search for disabled objects. For that you need to roll your own find.
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
}
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
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
This checks if the variable has been defined, I need to check if the prefab exists (if it is in the hierarchy).
Do you mean that you want to check whether it has been instantiated correctly? How do you instantiate it at the moment?
Right I think you need to do this:
if (WeaponViewPrefab.activeInHierarchy) { // Do something }
This returns true if it is active in the scene.
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++;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613104404im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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