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 /
avatar image
0
Question by Raylee01 · Aug 19, 2017 at 11:28 AM · error2d gameendless runnerfindgameobjectswithtag

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");

} }

Comment
Add comment · Show 1
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
avatar image ShadyProductions · Aug 19, 2017 at 02:13 PM 0
Share

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
 }

0 Replies

· Add your reply
  • Sort: 

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

92 People are following this question.

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

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


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