- Home /
FindObjectsOfType()
hi i was just wandering if it is possible to find find the objects of type except from one perticular object type
Can you elaborate? FindObjectsOfType only returns the objects of one type, so which type are you trying to exclude?
yea the problem is all objects pretty much have a type of some sort. returning all objects except one type would give you everything in the scene but that.
Ah, you want everything except one type? I'll give it some thought but I can say that that's not very good OOP/design. If you can do it another way, you should.
i suppose i can put all sfx in empty gameobjects and tag them SFX (probably best for me at my current code knowledge) here is my code which i was helped with sorting out my array problem, its not the script im going to use but its what i need it to do
pragma strict
var sources : AudioSource[]; var originalVolumes : float[];
function Start(){ var sourceGOs = FindObjectsOfType(AudioSource);//except from gameobject.find("master").audiosource // var sourceGOs = GameObject.FindGameObjectsWithTag("SFX"); originalVolumes = new float[sourceGOs.length];
sources = new AudioSource[sourceGOs.Length];
for(var i = 0; i < sources.Length; i++){
sources[i] = sourceGOs[i].audio;
originalVolumes[i] = sources[i].volume;
}
} var vol : float;
var boo : boolean = false; var object : Transform;
function Update(){
for(var i = 0; i < sources.Length; i++){
sources[i].volume = originalVolumes[i] * vol;
}
if(boo == true){ boo = false; Instantiate(object);
}
}
i have a seperate script on each instantiated object to control their sound from this script, but as i said this is only for testing
Answer by whydoidoit · Aug 20, 2012 at 04:20 PM
Sure you can do that using Linq:
CS:
using System.Linq;
var myObjects = GameObject.FindObjectsOfType(typeof(SomeType)).Cast<SomeType>().Where (obj => !(obj is TheTypeToAvoid)).ToList();
This Linq query will go trough all Components on all hierarchy Objects and return the Objects that have that Component. Did I get it right?
No, you just described the normal behavior of FindObjectsOfType (well, kinda).
$$anonymous$$ikes solution will go trough all Components on all hierarchy Objects and return the components/objects that DONT match the type.
Well FindObjectsOfType will create the initial set based on its normal operation (everything active in the scene that is derived from SomeType) and then the Linq just removes the ones that are of the type to be avoided.
@khada sorry looks like we were typing at the same moment!
I dont see what its function is? You only get a list containing one type of object/component, what will checking it for other type(s) do?
Answer by sparkzbarca · Aug 20, 2012 at 04:48 PM
I'm guessing but I believe what you probably mean is you have for example weapons and some are rifles and some are shotguns and some are rocket launchers and some are machine guns. you'd like to get everything BUT the machineguns. What you have there is a base type and a bunch of sub types but you'd like to exclude 1 subtype while returning all others. the way in comments does it in one line.
If that is what you want one way you could do it would be to create a create an array and have it be all the objects of the base.
so
weapon : weapons_array = findobjectsoftype(weapon)
now you want to go through the array and remove all items whose subtype are equal to the excluded subtype
so
for(iterator; iterator < weapons_array.size(); iterator++)
{ if (weapons_array[iterator].subtype == "machine gun"
weapons_array[iterator].delete()
}
now the list is everything except machine guns.
hope that helps
dont know if that will work for me as my array is builtin and is an array of floats and audiosources, even though im trying to exclude one objects audiosource