Y
Y
Yura Berezovsky2017-04-24 20:51:22
C++ / C#
Yura Berezovsky, 2017-04-24 20:51:22

Why is the object not activated after the ***.gameObject.SetActive(true); function?

The script was supposed to deactivate child objects at startup (which it successfully does), and after the collide with objects of the Ground, Side, LeftSide tag, activate it back (which it does not successfully do).

using UnityEngine;
using System.Collections;

public class EnablingColliders : MonoBehaviour
{
    private Transform childObj;
    private Transform childObjSecond;
  // Use this for initialization
  void Start ()
  {
      childObj = transform.FindChild("Collider");
      childObjSecond = transform.FindChild("Effectors");
        childObj.gameObject.SetActive(false);
        childObjSecond.gameObject.SetActive(false);
        
  }
  
  // Update is called once per frame
  void Update () {
  
  }

    void OnCollisionEnter2D(Collider2D collision)
    {
        if (collision.transform.tag == "Side" || collision.transform.tag == "LeftSide" || collision.transform.tag =="Ground")
        {
            childObj.gameObject.SetActive(true);
            childObjSecond.gameObject.SetActive(true);

        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2017-04-24
@Jurajkeee

childObj.FindObject() is essentially hidden behind the call to childObj.gameObject , and the search will not find disabled objects.
Save the reference directly to the GameObject , not to the Transform

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question