Spawning game piece correctly to allow highlighting
I am trying to make a board game and I have made some progress so far, being able to spawn game pieces onto various transforms (serving as my board grid cells). Next, I am trying to highlight each piece once I click/select them and then deselect in a similar manner. I am creating a highlight effect by placing 2 game pieces (spheres) in the same physical space, but my "highlighted" version is set to inactive. I have even created a toggle between the two, but my problem is that when I am spawning new pieces the new ones do not allow themselves to be recognized as game pieces and so only the original piece on the board is able to be clicked still.
Here is my spawn:
public GameObject prefab;
public GameObject Highlight;
//public Transform[] spawnPoints;
public List<Transform> spawnPoints;
// Click the "Instantiate!" button and a new `prefab` will be instantiated
// somewhere within -10.0 and 10.0 (inclusive) on the x-z plane
/*void OnGUI()
{
if (GUI.Button(new Rect(10, 10, 100, 50), "Instantiate!"))
{
var position = new Vector3(Random.Range(-10.0f, 10.0f), 0, Random.Range(-10.0f, 10.0f));
Instantiate(prefab, position, Quaternion.identity);
}
}*/
void OnGUI()
{
//int spawn = Random.Range(0, spawnPoints.Length);
int spawn = Random.Range(0, spawnPoints.Count);
if (GUI.Button(new Rect(10, 10, 100, 50), "Instantiate!"))
{
//var position = new Vector3(Random.Range(-10.0f, 10.0f), 0, Random.Range(-10.0f, 10.0f));
var SpawnedPiece = Instantiate(prefab, spawnPoints[spawn].transform.position, Quaternion.identity);
var Highlighted = Instantiate(Highlight, spawnPoints[spawn].transform.position, Quaternion.identity);
Highlighted.transform.SetParent(SpawnedPiece.transform);
Highlighted.SetActive(false);
SpawnedPiece.tag = "Game Piece";
Highlighted.tag = "Game Piece";
spawnPoints.RemoveAt(spawn);
}
}
Here is my highlight:
public GameObject GamePiece;
public GameObject Highlighted;
public int minRandom;
public int maxRandom;
// Start is called before the first frame update
void Start()
{
GamePiece.gameObject.SetActive(true);
Highlighted.gameObject.SetActive(false);
//AddPhysics2DRaycaster();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
// whatever tag you are looking for on your game object
if (hit.collider.tag == "Game Piece")
{
Debug.Log("---> Hit: ");
StartCoroutine(Waiter());
}
else
{
StartCoroutine(GoBack());
}
}
}
IEnumerator Waiter()
{
int wait_time = Random.Range(minRandom, maxRandom);
yield return new WaitForSeconds(wait_time);
//print("I waited for " + wait_time + "sec");
GamePiece.gameObject.SetActive(false);
Highlighted.gameObject.SetActive(true);
}
IEnumerator GoBack()
{
int wait_time = Random.Range(minRandom, maxRandom);
yield return new WaitForSeconds(wait_time);
//print("I waited for " + wait_time + "sec");
GamePiece.gameObject.SetActive(true);
Highlighted.gameObject.SetActive(false);
}
}
There might be bracket issues above, but that is just because this is pasted from mobile, there are no errors on computer. Can anyone tell me why my other game pieces are not highlighting?
Your answer
Follow this Question
Related Questions
Instantiate PreFab on trigger location 0 Answers
UI Button text highlighted 1 Answer
Object dissapears on MoveTowards 1 Answer