0
\$\begingroup\$

I've been really stuck on this one and need some help.

I've got some accessories for my player, and my goal is to have a collider, with the script attached. When the player walks into the script, the inactive model components on the player, will be set to active.

This is the script I'm using:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemEquip : MonoBehaviour
{
    public GameObject[] Apparel;
    public bool SetActive;

    void Start()
    {
        Apparel = GameObject.FindGameObjectsWithTag("Apparel");
    }

    void OnTriggerEnter(Collider ApparelCollect)
    {
        if (ApparelCollect.CompareTag("Player"))
        {
            GameObject[] clothes = GameObject.FindGameObjectsWithTag("Apparel");
            foreach (GameObject apparel in clothes)
            {
                apparel.SetActive(true);
            }
        }
    }
}

I'm really stuck on this. It's something basic but despite trying several tutorials, asking some people I know and experimenting, nothing is getting this script to work.

What I've tried: -Deactivating the individual components -Attaching a script to each component, along with the trigger -Activating the individual components (from inspector) -Activating the script (from inspector) -Double checking that each component I want affected has the Apparel tag -Cry in a corner

I've been going around in a loop for ages trying to get this thing to work, but still nothing.

I'm self taught on game development, working on my first game. I've read the Unity manual (that absolutely did not help) as well as hit up YouTube and multiple forums, all with no luck.

\$\endgroup\$
2
  • \$\begingroup\$ Seems you forgot to add your script to the question. \$\endgroup\$ Commented Feb 1, 2020 at 2:37
  • \$\begingroup\$ @disc_code22 Thank you, I've now included it. It's been one of those days -_-; \$\endgroup\$ Commented Feb 1, 2020 at 3:17

1 Answer 1

1
\$\begingroup\$

FindGameObjectsWithTag only finds already active GameObjects. From the documentation:

Returns an array of active GameObjects tagged tag. Returns empty array if no GameObject was found.

So it won’t work to turn on a collection of inactive objects. You’re going to have to figure out a different way to find all the apparel. You might try adding an empty Apparel script to each item of apparel and getting all instances of it, eg.

void OnTriggerEnter(Collider ApparelCollect)
{
    if (ApparelCollect.CompareTag("Player"))
    {
        Apparel[] clothes = ApparelCollect.gameObject.GetComponentsInChildren<Apparel>(true);
        foreach (Apparel apparel in clothes)
        {
            apparel.gameObject.SetActive(true);
        }
    }
}

Note that this assumes that the apparel are all children of the collider that hit the trigger. If that’s not the case, you’ll need a more complex solution.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.