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 Amaresh · Jul 18, 2014 at 04:12 PM · gameobjectlistloopname

How to obtain a gameObject from an List with the name?

Hey Guys! I'm a bit stuck here. I've a List through which I'm looping and checking to see whether the object I've send through the argument is active or not. If not I'm activating it.

But what I want is to check whether the gameObject with the same name as I'm sending to it is active or not. Is there a way I can do this or not.

Here is my code: public GameObject activateObstacle(int id, string name){

         for (int i = 0; i < obstaclePool[id].Count; i++) 
         {
             if(!obstaclePool[id][i].activeInHierarchy)
             {
                 obstaclePool[id][i].SetActive(true);
 
                 string spawnedObstacleName = obstaclePool[id][i].gameObject.name;
                 
                 Debug.Log("spawned obstacle name is " + spawnedObstacleName);
 
                 return obstaclePool[id][i];
             }        
         }
         }

I want to check whether spawnedObstacleName is similar to the name parameter that I'm sending. And If it is same then do all the stuff like checking whether it is active or not and then activating it.

Please Help. Thank you.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Loius · Jul 18, 2014 at 04:18 PM

 if ( name == spawnedObstacleName ) {
   // stuff
 }


That said, don't use object names. It's extremely unreliable because someone will make a typo at some point and it will be extremely hard to track down why that's causing an error.

Use custom components with an identifier (like index) if you have to, but names are a last resort.

In your case you want to store the obstacles in a list so you can just send the index and simply not care what the name is.

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 Amaresh · Jul 18, 2014 at 04:26 PM 0
Share

@Lolus I've tried that inside the forloop earlier. But it didn't work. not going inside the that if statement.

avatar image Loius · Jul 20, 2014 at 06:13 PM 0
Share

use debug.log to print out the names of the objects. if you think there should be a match and there isn't then you should make sure that you're passing in the right name and all.

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

22 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

Related Questions

A node in a childnode? 1 Answer

Problem to rename gameobject when loading from file. 0 Answers

Find Many GameObjects by name 2 Answers

GameObject.find("name") doesn't work if executed in the same loop of... 1 Answer

Looping through multiple lists 0 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