• 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 AngryCSharpNerd · Mar 08, 2013 at 03:39 AM · variableid

Finding a GameObject by a scripted variable

So I have this crazy text RPG thing, and I'm trying to create a library of "weapons", which are basically just empty GameObjects with variables attached to them by a script called Item_Weapon.js, and they'll only mean something when they are used by a script.

So, I have a variable called "ID" attached to the "weapons". I'm trying to make it like as if I could do like a GameObject.FindInTheLibrary(ID Number). How is it possible to find a GameObject in the scene by a custom variable?

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
Best Answer

Answer by robertbu · Mar 08, 2013 at 06:50 AM

If you have the same script attached to all weapons (you can have other scripts as well), you should be able to use Object.FindObjectsOfType() to get a list of those components. Then you will need to cycle through them to find the one you want.

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 AngryCSharpNerd · Mar 08, 2013 at 12:22 PM 0
Share

Okay, so I've thought of another method ins$$anonymous$$d of ID, and it almost works. How do I tell a script if it has no child? "!transform.Find()" even though it doesn't work, I want it to mean like "if I don't have any child attached to me, then"

avatar image robertbu · Mar 08, 2013 at 03:20 PM 0
Share

How about Transform.childCount

avatar image
0

Answer by Kleptomaniac · Mar 08, 2013 at 06:35 AM

Hey there,

All you need to do is store your ID within tags assigned to your individual GameObjects like so: gameObject.tag = ID; and then use GameObject.FindGameObjectsWithTag() to find your GameObject: GameObject.FindGameObjectsWithTag(ID);

Hope that helps! :)

Klep

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

11 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

Related Questions

Adding id variable # to instantiate-ed object 2 Answers

how to Stream.serialize(stringValue) 2 Answers

Notes or Bubble Window for Variables in Inspector Giving Help? 3 Answers

Updating variable on another script. It is not working. 2 Answers

Add a Prefix to a Variable 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