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?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnableWeapons : MonoBehaviour
{
// Start is called before the first frame update
void Awake()
{
GameObject[] objs = GameObject.FindGameObjectsWithTag("PowerupsActive");
if (objs.Length == 1)
{
GameObject.FindGameObjectWithTag("Weaponry").SetActive(true);
}
}
}
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.
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.
I get the idea, but I’m not sure how to write it out
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnableWeapons : MonoBehaviour
{
public GameObject weapon;
public GameObject[] weapons;
void Start()
{
if (weapons == null)
weapons = GameObject.FindGameObjectsWithTag("Weaponry");
foreach (GameObject weapon in weapons)
{
weapon.SetActive(true);
}
}
}