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.
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));
}
}
}