Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Setting objects with a tag active

Discussion in 'Scripting' started by rudigreig, Jun 1, 2020.

  1. rudigreig

    rudigreig

    Joined:
    Dec 11, 2018
    Posts:
    50
    I'm making a racing game, one of the modes is a battle mode where the cars have weapons attached. I thought a simple way to do this was to activate all objects with the tag "Weaponry" (the weapons) when there is an object with the tag "PowerupsActive" in the scene, what's wrong with my script?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnableWeapons : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Awake()
    9.     {
    10.         GameObject[] objs = GameObject.FindGameObjectsWithTag("PowerupsActive");
    11.         if (objs.Length == 1)
    12.         {
    13.             GameObject.FindGameObjectWithTag("Weaponry").SetActive(true);
    14.         }
    15.          
    16.  
    17.     }
    18.  
    19.  
    20. }
     
    Last edited: Jun 1, 2020
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    FindGameObjectWithTag() only returns a single object. You need the slightly different FindGameObjectsWithTag(). You probably made a typo. You will also need to iterate over all the objects in the returned array with a loop to set each of them active.
     
  3. rudigreig

    rudigreig

    Joined:
    Dec 11, 2018
    Posts:
    50
    How would I do this? Sorry, don't have much scripting knowledge
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Check out https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

    There are two example code snippets there. The first code snippet calls FindGameObjectsWithTag, then it uses a foreach loop to iterate over all the results. Try making a foreach loop similar to the example, and set each object to active instead of whatever they're doing in the example.
     
    rudigreig likes this.
  5. rudigreig

    rudigreig

    Joined:
    Dec 11, 2018
    Posts:
    50
    I get the idea, but I'm not sure how to write it out
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnableWeapons : MonoBehaviour
    6. {
    7.     public GameObject weapon;
    8.     public GameObject[] weapons;
    9.     void Start()
    10.     {
    11.         if (weapons == null)
    12.             weapons = GameObject.FindGameObjectsWithTag("Weaponry");
    13.  
    14.         foreach (GameObject weapon in weapons)
    15.         {
    16.             weapon.SetActive(true);
    17.         }
    18.     }
    19. }
    20.  
     
  6. rudigreig

    rudigreig

    Joined:
    Dec 11, 2018
    Posts:
    50
    I fixed the problem, I did this by doing the opposite- disabling them when I didn't need them
    Code (CSharp):
    1. public GameObject[] weapons;
    2.     void Update()
    3.     {
    4.         weapons = GameObject.FindGameObjectsWithTag("Weaponry");
    5.  
    6.         foreach (GameObject weapon in weapons)
    7.         {
    8.             weapon.SetActive(false);
    9.         }
    10.     }