Hi,
I’m making a sort of strategy game and I’m currently working on the selection and moving stuff, but sadly I have encountered a problem that I’ve been unable to solve. In the game I have 4 balls that I have manually put there and a number of instantiated ball all made from the same prefab (every ball is perfectly identical). And what’s happening is that I can select and deselect (change a bool from false to true) the manually put balls just fine but nothing happens in the instantiated balls. Even if I press one of the instantiated balls, only the manually placed balls are selected.
To spawn the balls I currently use either this:
void Start()
{
//for (int i = 0; i < 1000; i++) {
// Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
//}
}
Or this:
void Start ()
{
GameObject newObject = (GameObject)Instantiate (PALLOPrefab);
newObject.GetComponent ().valittu = true;
}
And this is where the magic doesn’t happen, I think. The bool “valittu” (chosen) determines whether the object is selected or not:
void Update()
{
if (Input.GetMouseButtonDown (0)) {
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10)), Vector2.zero);
if (hit.collider != null) {
if (hit.transform.tag == "Unit1") {
Debug.Log("lol");
foreach (GameObject go in GameObject.FindGameObjectsWithTag("Unit1")) {
go.GetComponent<Ukko> ().valittu = true;
valittu=true;
go.GetComponent<Ukko> ().novoijuma = true;
}
}
}
}
This script is attached to an object responsible for controlling stuff and each ball has some other script. I’m quite sure that the cause is in the “foreach” bit, since the “lol” prints out just fine when clicking on Instantiated ball, but I have no idea how to fix it.
Thanks for help!