The question is answered, right answer was accepted
How to access an inactive gameObject with tag
Hello. I have a gameObject called playerWeaponsPrefab which has 4 children, the first child is tagged as PrimaryWeapon, the second is tagged as SecondaryWeapon, the third is tagged as Knife and the forth is a grenade, I want to select these weapons in game by pressing 1,2,3 and 4 buttons ( while a weapon is selected others must be inactive). I put these weapon gameObjects in 4 variables and used GameObject.FindwithTag(exampleWeapon) for accessing to them but I can not access to them and it returns null because the gameObject is not active. I was shocked when I saw GameObject.FindWithTag doesn't work on inactive Objects. what Should I do to access my inactive weapon? thanks for help in advance.
Answer by Safforn · Jun 15, 2016 at 01:12 PM
Leave them enabled in the editor, get them with Start() and after you have them disable them.
You could also try making 4 public gameObjects and just drag your weapons in there.
private void Start()
{
weapon = GameObject.FindGameObjectWithTag("tag");
weapon.SetActive (false);
}
Nice! Could you mark the question as answered and accept my answer?
Answer by Arshd · Jun 15, 2016 at 01:37 PM
You should have a pre-defined array with the weapons or a list if you want to add more in the future, i think it's better than always using any GameObject.Find
Yeah I agree, that would be more efficient. He could also just make his gameobjects public and assign them from the inspector. Just drag and drop
He could and that what i meant sorry if i wasn't clear :)
Follow this Question
Related Questions
instantiate prefab at every gameobject with tag 0 Answers
How Can I assign a gameobject via script? 0 Answers
What to use instead of GameObject.Find and GetComponent 2 Answers
Stop a game object from following an untagged object. 1 Answer
How to become invincible towards enemies when powerup is active 2 Answers