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
1
Question by nikofon007 · Sep 01, 2020 at 07:17 PM · instantiateprefabsearch

Search for a prefab with certain property

I have a bunch of prefabs of gameobjects with same attached class. This class contains an identification property (each prefab has different value of this property). I want to search for prefab with certain value of this property and instantiate it. If I can't do it with prefabs, can I do it (it = get gameobject by property of an attached class) with already instantiated gameobjects? (and if I can is this the way to do it? -> https://docs.microsoft.com/en-us/previous-versions/windows/desktop/wmi_v2/how-to-get-a-specific-instance-of-a-class)

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Nimi142 · Sep 01, 2020 at 10:36 PM

Won't doing:

 GameObject prefab; // The prefab you have.
 ComponentYouWant component = prefab.GetComponent<ComponentYouWant>(); // The component you want to check
 if (component != null && component.valueYouWant == something) Success;

Work? That would take the prefab (with the components and stuff you configured for it on the inspector), and try to get the component you want. If the prefab doesn't have the component, then it will be null. If it does, then the other type of the if statement will come into practice and you'll be able to check if it satisfies your goals.

You can do it as many times you want for as many prefabs and components you want. You can also use the HasComponent<>(); function.

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
avatar image
0

Answer by TomArano_Dimenco · Sep 02, 2020 at 09:24 AM

https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html

is this what you would need? or are the prefabs not in the scene yet?

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 ShadyProductions · Sep 02, 2020 at 09:35 AM 0
Share

^ This would work if the objects are in the scene indeed,

@nikofon007

Otherwise you could simply assign the prefabs you need in the inspector.

 public GameObject[] prefabs;
 
 // identification can be anything
 public GameObject GetPrefab(string identification)
 {
     foreach (var prefab in prefabs)
     {
         var comp = prefab.GetComponent<YourScript>();
         if (comp.identification == identification) return prefab;
         //you could return comp here too
     }
     return null;
 }
avatar image TomArano_Dimenco ShadyProductions · Sep 02, 2020 at 10:19 AM 0
Share

Yes! U can assign prefabs to an array through inspector and just look through that array to what u need. u wont have to load resources this way, making it easier.

avatar image gaaraofdesert9 TomArano_Dimenco · Oct 20, 2020 at 06:26 PM 0
Share

Thank you, @ShadyProductions !! Took a little bit of brain power on my part and I had to think through the function several times (came close to caving and just asking), but I understand what is happening here now, and this is exactly what I needed. :)

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

195 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot find 3D Prefab after doing a build. 0 Answers

Instantiate moving prefab 1 Answer

How to check if a component is on an instantiated prefab? 1 Answer

Lobbing a Bomb with Instantiate. 1 Answer

Randomly pick then create prefab 2 Answers


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