• 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 Crispinator · Oct 17, 2017 at 06:08 PM · variabletagstargettargetingtargetting

Choose a Random Tag

Basically, I have five GameObjects each with a tag that is a number between 1 and 5. I'm trying to set the tagNumber integer = a random integer between 1 and 5. Then, I want it to set the target variable equel to a GameObject with the randomly selected tag. Any help is much appreciated.

 Transform target;
 public int tagNumber;
 
 
 void Awake () {
         tagNumber = Random.Range (1, 5);
         target = GameObject.FindWithTag (tagNumber).transform;
 
     }

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 PizzaPie · Oct 18, 2017 at 11:46 AM 0
Share

or just change it to this:

 tagNumber = Random.Range (1, 6);
 target = GameObject.FindWithTag (tagNumber.ToString()).transform;

1 Reply

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

Answer by pako · Oct 17, 2017 at 06:36 PM

GameObject.FindWithTag(string tag) needs a string parameter, not an int.

  1. Create a string array to hold your tags e.g. string[] tagsArray = new string[5] //to hold 5 tags

  2. You can declare the array as public and enter the tag strings in the Inspector, or declare them in the array initializer.

  3. Use Random.Range to get a random index for the array e.g. for 5 tags the array length is 5, with indeces 0 to 4. So, use randomIndex = Random.Range (0, 5) //5 is excluded

  4. Then: string randomTag = tagsArray[randomIndex]

  5. Finally, target = GameObject.FindWithTag (randomTag).transform

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 Crispinator · Oct 17, 2017 at 10:11 PM 0
Share

At which point do I declare the "randomIndex" variable?

avatar image pako Crispinator · Oct 18, 2017 at 09:21 AM 1
Share

Sorry I overlooked that. You can declare it inline, i.e. int randomIndex = Random.Range (0, 5);

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

77 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

Related Questions

Targetting script bug. (Javascript) 0 Answers

Pick the oldest object from an array. 2 Answers

2d AI: enemy targeting c# 2 Answers

Coding equivalent of Draging an Object into the Inspector (Solved) 1 Answer

Floor Halo Target System 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