Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DJGhostViper · Jan 04, 2017 at 10:19 PM · gameobjectlistnumber

Checking if a gameobject is at a specfic # in a list

I have a list and I'm trying to check if a gameobject is at a certain place in that list but I not sure how to check it .

 public List<GameObject> Buttons = new List<GameObject>();
 
 
 
 if (this.CompareTag ("Barracks") && this.gameObject == Buttons[2].gameObject) {
     
 //Some code            
 
 }
 
 

Above just checks if the list[#].gameobject is the same type of gameobject as this gameobject but it doesnt check if its this specific gameobject. Thanks!!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by KringleHeim · Jan 05, 2017 at 06:08 PM

"Above just checks if the list[#].gameobject is the same type of gameobject as this gameobject but it doesnt check if its this specific gameobject". No, it doesn't :) It check if it is the very same variable (unless it's a primitive type (int, float...), string etc). To see this, try something similar to my code here, where the other gameObject is of your choosing. Point is that although they are both of the base class, GameObject, the == operator returns false.

   GameObject thisOne;
     GameObject otherOne;
     void Start () {
         thisOne = this.gameObject;
         otherOne = GameObject.FindGameObjectWithTag("MainCamera");
         Debug.Log((thisOne == otherOne).ToString());
         Debug.Log(thisOne.GetType());
         Debug.Log(otherOne.GetType());
 
     }

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 LK84 · Jan 04, 2017 at 10:34 PM

You want to get the index of the GameObject in the List? Use List.FindIndex https://msdn.microsoft.com/de-de/library/x1xzf2ca(v=vs.110).aspx

 int index =Buttons.FindIndex(x=>x==gameObject);
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 KoenigX3 · Jan 05, 2017 at 10:26 AM

I think the operator '==' should work well in this situation, since it checks whether the two references (on the left and right side of the operator) refer to the same object.

Do you want to check if the gameobject (which has this script attached to it) is the same as the gameobject, which is in the Buttons list at the index of 2?

Comment
Add comment · Show 2 · 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 DJGhostViper · Jan 05, 2017 at 11:51 AM 0
Share

Yes exactly @$$anonymous$$oenigX3

avatar image KoenigX3 · Jan 05, 2017 at 04:37 PM 0
Share

Then it should return 'true' if they are referring to the same object. How do you know that the code above checks the types? What is the problem with the code above?

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

80 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

Related Questions

A node in a childnode? 1 Answer

Add and destroy instantiated gameobject in linkedlist C# 1 Answer

When I add a gameObject from one list to another, it makes a new one instead of referring to the same one? 0 Answers

Enabled all gameobjects in array 1 Answer

Getting a List of Gameobjects in a collider 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