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 /
  • Help Room /
avatar image
0
Question by pgrenon · Feb 25, 2017 at 05:05 AM · c#findgameobjectswithtag

FindGameObjectsWithTag and then find a variable C#

Is there something like a "Find Game Object That Has a Certain Value in a Variable" to find a specific object?

I am UBER noob at Unity and coding, I only do this as a hobby. So please be very specific if you answer :). I am trying to make a little game where the player navigates through tiles and gains resources and spends them to move, etc.

Here is what it looks like in Game View: http://imgur.com/RhxdImS

This is the script attached to all my tiles that 1. Turns the original selected tile to red. 2. When you click on a tile to select it, it turns the one that was previously selected to white and 3. It turns the one you just clicked on to red.

In C#:

 using UnityEngine;
 using System.Collections;
 
 public class TileScript : MonoBehaviour
 {
     void Start()
     {   
         // put color of the first tile to red
         if (gameObject.tag == "Selected Tile")
         {
             GetComponent<Renderer>().material.color = Color.red;
         }
     }
 
     GameObject SelectedTile;
 
     void OnMouseDown()
     {
         // change tag from Selected Tile to Tile
         SelectedTile = GameObject.FindGameObjectWithTag("Selected Tile");
         SelectedTile.gameObject.tag = "Tile";
         SelectedTile.gameObject.GetComponent<Renderer>().material.color = Color.white;
 
         // change tag to Selected Tile
         gameObject.tag = "Selected Tile";
 
         // change color according to tag
         if (gameObject.tag == "Selected Tile")
         {
             GetComponent<Renderer>().material.color = Color.red;
         }
     }
 }

I understand that Unity objects have tags, which help to identify them in codes. Right now, I have two tags: "Tile" and "Selected Tile". This is what my script uses to identify which game object is the "Selected Tile".

The problem is that I would like to use these tags to determine whether a tile is, for example, a "Forest Tile" or a "Mountain Tile" or a "Valley Tile" in order to code the effect that each tile has on the player when the player arrives on a tile. i.e. [the selection].

To do this, I need to make the selection process use a different method than by tag, so I imagine that creating a boolean variable "Selected" in each object would be a good way? Now I do know how to find an object by its tag or its name, but I do not know how to find an object by the value of a variable that is in a script component of a game object.

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 Pengocat · Feb 25, 2017 at 07:41 AM

You could use the static modifier. When something is static it no longer belongs to any particular instance but is shared across all instances. In my example code you can ignore the OnGUI() method since it is only there to visualize the values. This example also stores the original colour instead of just making it white.

 using UnityEngine;
 
 public class TileScript : MonoBehaviour
 {
     private static TileScript selectedTile;
     private static Color selectedColor;
     private static int forestPoints;
     private static int valleyPoints;
     private static int mountainPoints;
 
     private Material tileMaterial;
     private Color originalColor;
 
     // Awake is called when the script instance is being loaded
     void Awake()
     {
         // Get a reference to the material of this
         tileMaterial = GetComponent<Renderer>().material;
         // Save the original material color of this
         originalColor = tileMaterial.color;
         // Set the selected Color to red
         selectedColor = Color.red;
     }
 
     // OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider
     void OnMouseDown()
     {
         // If the same tile is clicked again stop this method
         if (selectedTile == this) return;
 
         DeselectSelectedTile();
 
         // Set this tile color to the selected Color
         tileMaterial.color = selectedColor;
 
         if (CompareTag("Forest"))
         {
             forestPoints += 10;
         }
         else if (CompareTag("Mountain"))
         {
             mountainPoints += 10;
         }
         else if (CompareTag("Valley"))
         {
             valleyPoints += 10;
         }
 
         // Set this as the current selected tile
         selectedTile = this;
     }
 
     /// <summary>
     /// Sets the selectedTile back to its original color
     /// Sets selectedTile to null
     /// </summary>
     private static void DeselectSelectedTile()
     {
         // If the selectionTile is null don't try to set a color
         if (selectedTile != null)
         {
             // Set the selected tile back to its original color
             selectedTile.tileMaterial.color = selectedTile.originalColor;
             selectedTile = null;
         }
     }
 
     // OnGUI is called for rendering and handling GUI events
     void OnGUI()
     {
         if (this == selectedTile)
         {
             GUI.Label(new Rect(0, 0, 300, 20), 
                 string.Format("forest points = {0}", forestPoints));
             GUI.Label(new Rect(0, 20, 300, 20), 
                 string.Format("mountain points = {0}", mountainPoints));
             GUI.Label(new Rect(0, 40, 300, 20), 
                 string.Format("valley points = {0}", valleyPoints));
         }
     }
 }
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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Casting FindGameObjectsByTag to another type 1 Answer

Referencing variable from another script on another object 3 Answers

How to use 2 tags in FindGameObjectsWithTag. 2 Answers

GameObject[] doesn't have a definition for transform/name/etc... 1 Answer

Get Childrens to new array from perents array. 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