• 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 Bouldoro · Jun 17, 2018 at 12:42 PM · findgameobjectswithtagfindobjectsoftype

How do I find a GameObject by a script attached to it?

I'm currently using FindGameObjectsWithTag(""). I'm trying to use FindObjectsOfType<>() to the same thing with a script attached to it instead of a tag (since no multiple tags).

 public List<GameObject> isObject;
     GameObject go;
 
     void Start () {
 //isObject.AddRange(GameObject.FindGameObjectsWithTag("Player"));
         // Trying to find an object by component
         go = GameObject.FindObjectsOfType<IsObject>();
         isObject.AddRange(go);
 
         isObject.Clear();
     }


I get the errors

Assets/Scripts/IsObject.cs(15,21): error CS1503: Argument #1' cannot convert >UnityEngine.GameObject' expression to type >`System.Collections.Generic.IEnumerable'

and

Assets/Scripts/IsObject.cs(15,21): error CS1503: Argument #1' cannot convert >UnityEngine.GameObject' expression to type >`System.Collections.Generic.IEnumerable'

Comment
Add comment · Show 1
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 Zgull · Jun 17, 2018 at 12:54 PM 0
Share

Are you trying to refer all game objects added in your scene?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Jun 17, 2018 at 01:45 PM

You don't need to find anything. Every component instance already knows which gameobject it belongs to. You can access the owning GameObject by using the gameObject property of your component.

 void Start ()
 {
     IsObject[] components = GameObject.FindObjectsOfType<IsObject>();
     foreach(IsObject comp in components)
         isObject.Add(comp.gameObject);
  }


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 Lost_Syndicate · Jun 17, 2018 at 04:16 PM 0
Share

I wouldnt recommend using FindObjectsOfType in your final game.

avatar image
0

Answer by Bouldoro · Jun 18, 2018 at 06:19 AM

To follow up, can I add a condition to that?

I only need objects that are above me (the object that is found). For example I have 5 objects lined up vertically

 o(1) has 0 objects above it
 o(2) has 1 object above it
 o(3) has 2 objects above it
 o(4) has 3 objects above it
 o(5) has 4 objects above it

I also want to keep doing this (not just at the start). Since I'm not applying forces I should place this in FixedUpdate List isObject; IsObject[] listOfObjects; SpriteRenderer sr;

 void FixedUpdate () {
         listOfObjects = GameObject.FindObjectsOfType<IsObject> ();
         sr = GetComponent<SpriteRenderer> ();
     }
 void FixedUpdate () {    
         foreach(IsObject foundObject in listOfObjects){
             if (foundObject.transform.position.y > transform.position.y){
                 isObject.Add (foundObject.gameObject);
                 sr.sortingOrder = isObject.Count;
             }
         }
 
         isObject.Clear();
     }

When I moved to fixedupdate i get the error

NullReferenceException: Object reference not set to an instance of an object IsObject.FixedUpdate () (at Assets/Scripts/IsObject.cs:36)

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

86 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

Related Questions

Magnetic Gameobject 1 Answer

HELP Find gameObject With tag in another array 1 Answer

Applying Force to Game Objects with Tag Errors. 1 Answer

Better solution for GameObject.FindGameObjectsWithTag 2 Answers

How to get if all all objects are destroyed to start another wave 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