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
}
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
}
Ok, I found it:
var WeaponViewPrefab : GameObject;
if (GameObject.Find(WeaponViewPrefab.name) != null) {
//Do something
}
Looks like I just answered my own question.
Look duplicate with this question :
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
}
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 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*.name.Contains("Terrorist Type")){*
closestTerrorist[k] = gameObjects*; // Pass the found Terrorists the the closestTerrorist[] array*
k++;
}
}
}