Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by bojan1986819 · Mar 12, 2015 at 05:45 AM · taggameobjectsactivatedeactivategameobject.tag

Activate/deactivate all GameObjects with same tag c#

I'm trying to create a script which would automatically activate and reactivate all GameObjects with the tag 'Flare' on them. 'flareOnTime' is for how long it is active and 'flareOffTime' is for how long it is inactive. I'm stuck right now and cannot figure out what is wrong with my code. I would reaaly appriciate any help.

Code:

 using UnityEngine;
 using System.Collections;
 
 public class GameController : MonoBehaviour {
 
 
     public float flareOnTime;
     public float flareOffTime;
 
     GameObject[] gos;
 
 
 
     // Use this for initialization
     void Start () {
 
         GameObject[] gos = GameObject.FindGameObjectsWithTag("Flare");
 
     
 
         StartCoroutine("BlinkFlare");
 
 
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     IEnumerator BlinkFlare() {
 
         while (true) {
             yield return new WaitForSeconds (flareOnTime);
             foreach (GameObject go in gos){
             go.SetActive(false);
             }
             yield return new WaitForSeconds (flareOffTime);
             foreach (GameObject go in gos){
             go.SetActive(true);
             }
         }
     }
 
 }
 
Comment
Add comment · Show 3
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 AlwaysSunny · Mar 12, 2015 at 03:15 AM 0
Share

In what way does this not behave as expected? Please include that information when asking questions, thanks. :)

avatar image AlwaysSunny · Mar 12, 2015 at 03:17 AM 0
Share

$$anonymous$$aybe it's not an error, but there's no good reason you should start the coroutine with a string when you already know the name and are in the same scope.

 StartCoroutine(BlinkFlare());

Throw in some Debug.Logs to see if you can narrow down the culprit.

avatar image bojan1986819 · Mar 12, 2015 at 11:47 AM 0
Share

Sorry for not being specific enough, I try to do it better now(I'm very new to coding:) ). The problem with this script is the gameobjects with Flare tag are not blinking. I get the following error in console:

'NullReferenceException: Object reference not set to an instance of an object GameController+c__Iterator0.$$anonymous$$oveNext () (at Assets/Scripts/GameController.cs:41)'

GameController.cs is the script file where this script is.

I managed to make this script working previously but only for 1 GameObject. The difference was the I had a public GameObject named flare and the part of the code which is now

 'foreach (GameObject go in gos){
             go.SetActive(false/true);'

was simply

 'flare.SetActive(false/true);'

and like this it was working perfectly but only for the one object.

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Baste · Mar 12, 2015 at 11:59 AM

The error is that you're re-initalizing the array of GameObjects by doing this:

 GameObject[] gos = GameObject.FindGameObjectsWithTag("Flare");

instead of this:

 gos = GameObject.FindGameObjectsWithTag("Flare");

Essentially, when you write "MyClass foo = ", you're introducing a new variable named "foo" in the local scope, hiding any variable named foo in outer scopes - like class variables. So, in your start method, you're making a new array, and filling it with all of the objects tagged with "Flare", but you're not doing anything with it. The BlinkFlare method uses the empty game object field variable, and thus does nothing.

Comment
Add comment · Show 2 · Share
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 bojan1986819 · Mar 12, 2015 at 12:06 PM 0
Share

Thank you so much this was really the problem!

avatar image eovento · Sep 06, 2018 at 10:56 AM 1
Share

Thank you! Helped me too.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Cannot deactivate / activate gameobject, even in editor! 6 Answers

How to deactivate gameObjects instead of destroying them ? 1 Answer

CompareTag to multiple GameObjects 1 Answer

How to activate and deactivate a gameObject 1 Answer

problem with enable/disable on health script 1 Answer


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