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
0
Question by reptilebeats · Aug 20, 2012 at 03:44 PM · objecttype

FindObjectsOfType()

hi i was just wandering if it is possible to find find the objects of type except from one perticular object type

Comment
Add comment · Show 5
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 Khada · Aug 20, 2012 at 03:46 PM 0
Share

Can you elaborate? FindObjectsOfType only returns the objects of one type, so which type are you trying to exclude?

avatar image sparkzbarca · Aug 20, 2012 at 04:11 PM 0
Share

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.

avatar image Khada · Aug 20, 2012 at 04:16 PM 0
Share

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.

avatar image reptilebeats · Aug 20, 2012 at 05:13 PM 0
Share

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);

}

}

avatar image reptilebeats · Aug 20, 2012 at 05:15 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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();

Comment
Add comment · Show 11 · 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 Paulo-Henrique025 · Aug 20, 2012 at 04:28 PM 0
Share

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?

avatar image Khada · Aug 20, 2012 at 04:32 PM 1
Share

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.

avatar image whydoidoit · Aug 20, 2012 at 04:32 PM 0
Share

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.

avatar image whydoidoit · Aug 20, 2012 at 04:33 PM 0
Share

@khada sorry looks like we were typing at the same moment!

avatar image Khada · Aug 20, 2012 at 04:35 PM 0
Share

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?

Show more comments
avatar image
0

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

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 reptilebeats · Aug 20, 2012 at 05:04 PM 0
Share

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

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

11 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

Related Questions

Accessing properties from variable 1 Answer

'type mismatch' at custom inspector .asset 0 Answers

GetType is Monoscript, how to implement correctly? store type in variable? 1 Answer

Load Objects from a specific directory into a list? 0 Answers

AndroidJava and setting a BitSet value 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