Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 orangeflame · Aug 31, 2013 at 12:45 AM · arraylistconvertfind

Find GameObjects with a certain Script [Solved]

I know tags would probably be an easier way to search but I'm using them for other things and I basically want to create a list GameObject[] or Transform[] containing all object with a specific script called "Stat". I have tired to use FindObjectsOfType but that creates Object[] and I can't seem to find a way to convert object to transform or gameObject. I am posting the script that I'm trying to use to do this followed by the script that the first script is searching for. But first here is Snippet because this is a really long line of code. But first here is Snippet with the code in question because this is a really long line of code.

 public void AddAllEnemies(List<Transform> stuff)
         {
             //GameObject[] whatever = GameObject.FindGameObjectsWithTag("targetTag");
             //Object[] whatever = GameObject.FindObjectsOfType(typeof(Stat));
             GameObject[] whatever = FindObjectsOfType(typeof(Stat));
             
             foreach(GameObject enemy in whatever)
             {
                 AddTarget(enemy.transform, stuff);
             }
         }

The answer was:

     public void AddAllEnemies(List<Transform> stuff)
     {
         //GameObject[] whatever = GameObject.FindGameObjectsWithTag("targetTag");
         //Object[] whatever = GameObject.FindObjectsOfType(typeof(Stat));
         Stat[] whatever = FindObjectsOfType(typeof(Stat)) as Stat[];
         
         foreach(Stat enemy in whatever)
         {
             AddTarget(enemy.transform, stuff);
         }
     }
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 meat5000 ♦ · Aug 31, 2013 at 12:47 AM 1
Share

Total CODESTOR$$anonymous$$ man, no-one is going to look through this.

Please, strip out all the stuff that you $$anonymous$$NOW is not the issue and EDIT your question posting the updated code SNIPPETS

SNIPPETS!!!!

avatar image getyour411 · Aug 31, 2013 at 12:57 AM 1
Share

Had the same thought. This appears to be the area of concern:

  //GameObject[] whatever = GameObject.FindGameObjectsWithTag("targetTag");
 //Object[] whatever = GameObject.FindObjectsOfType(typeof(Stat));
 GameObject[] whatever = FindObjectsOfType(typeof(Stat));
avatar image meat5000 ♦ · Aug 31, 2013 at 01:00 AM 0
Share

Thumbs up for getting to line 595 before SHOOTING YOURSELF :D

I have to be honest I was so close to rejecting this from the moderation queue :D Everyone deserves a chance, right? :D

avatar image orangeflame · Aug 31, 2013 at 02:42 AM 0
Share

Sorry I had past problems and the first thing everyone said was don't just give me part of the code. I need to see all of it to whatever. Oh well I will cut out the main problem and throw it on top top and leave the rest for the people who need everything.

avatar image meat5000 ♦ · Aug 31, 2013 at 02:47 AM 0
Share

lol man thats hilarious :D

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by fred_gds · Aug 31, 2013 at 01:07 AM

Well FindObjectsOfType was the right answer. You can do something like this

var myScript : ScriptName[] = GameObject.FindObjectsOfType(typeof(ScriptName)) as ScriptName[];

for (var aScript : ScriptName in myScript) { //do whatever you want to }

This will give you all the scripts of it's kind in the scene. If you are look for a script named "stat" then just replace "ScriptName" with "stat".

I didn't read through your code as it was kind of too long ;) So I hope I got your problem anyways

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 orangeflame · Aug 31, 2013 at 03:01 AM 0
Share

That was almost right but I was able to finish the rest myself. The last thing I need to do was change.

         foreach(GameObject enemy in whatever)
         {
             AddTarget(enemy.transform, stuff);
         }

to

         foreach(Stat enemy in whatever)
         {
             AddTarget(enemy.transform, stuff);
         }

but still thank you. :D

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

20 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

Related Questions

Trouble with targeting enemies from a List 1 Answer

Is there an easy way to add an array of raycasthit2Ds to a list? (javascript) 2 Answers

Is it possible to convert c# generic List<> to normal arrays? 1 Answer

Emptying a Generic List / Unexpected Behaviour 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 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