- Home /
How to change variables inside FindGameObjectsWithTag objects
so i am instantiating bombs in different location and set their tag to "Bomb" and i want when the player scores more the speed of the bombs will increase so i made a switch statement in which i put the cases of the score and tried to change the speed of the gameobjects that has the tag "Bomb" i couldnt so i stored those bombs in gameobject array and tried again and i couldn't so i made an array of speed and made a for function in which i can assign to each gameobject a speed but unfortunatly i couldnt either and the last error was :
IndexOutOfRangeException: Array index is out of range. UIMANAGEMENT.Update () (at Assets/Scripts/UIMANAGEMENT.cs:33)
i made an if statement to detect if the bombs exists in the scene then start the for function but again i couldnt
so please help me make the bombspeed changes when the player scores higher score and thank you very much
here is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine;
public class UIMANAGEMENT : MonoBehaviour { public Text Paused;
public Text Score; public int[] BombSpeed; public bool right; public GameObject[] explosion; public bool left; public float PlayerScore;
void Start(){
}
public void Pause(){
if (Time.timeScale == 1) {
Time.timeScale = 0;
Paused.text = "Game Paused !";
} else if (Time.timeScale == 0) {
Time.timeScale = 1;
Paused.text = "";
}
}
void Update(){
explosion = GameObject.FindGameObjectsWithTag ("Bomb");
if (GameObject.FindWithTag ("Bomb")) {
for (int i = 0; i <= explosion.Length; i++) {
BombSpeed [i] = explosion [i].GetComponent<Explosion> ().speed;
switch ((int)PlayerScore) {
case 0:
BombSpeed [i] = 3;
break;
case 100:
BombSpeed [i] = 4;
break;
case 300:
BombSpeed [i] = 6;
break;
case 1000:
BombSpeed [i] = 8;
break;
case 1500:
BombSpeed [i] = 10;
break;
case 3000:
BombSpeed [i] = 12;
break;
case 5000:
BombSpeed [i] = 15;
break;
default:
break;
}
}
}
PlayerScore += 13 * Time.deltaTime;
Score.text = "Score: " + PlayerScore.ToString("0");
} }
You should use GameObject.FindGameObjectsWithTag ("Bomb");
in update, it's not good for performance.
Ins$$anonymous$$d when you instantiate the bombs add them to a list, and whenever you have to go through the list and change the speed:
foreach (var bomb in bombList) {
.GetComponent<Explosion> ().speed = 2; //etc
}
Your answer
Follow this Question
Related Questions
4 C# errors Endless Runner 0 Answers
Instantiating randomly sized prefab to the right of another 2D 0 Answers
Why can't my Physics.Raycast work 1 Answer
How to make a projectile in the finger/mouse position in a 2D endless runner? 0 Answers
cant play game because of InternalEngineBridge error 0 Answers