Search code examples
c#unity-game-enginegameobject

SetActive GameObject to true not working in Unity 5


Well I work with unity 5. I'm create a dialogue for interaction with other object. When the player is near of object I press return key for interaction.

The panel dialogue for start is disabled setActive(false).

In the method update when the player is near from other object and press return key and the panel setActive(true) , but this panel not enabled , I don't know why. Please Help.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

[RequireComponent(typeof(Text))]
public class Dialogue : MonoBehaviour
{   

    private Text _textComponent;

    public string[] DialogueStrings;

    public float SecondsBetweenCharacters = 0.15f;
    public float CharacterRateMultiplier = 0.5f;

    public KeyCode DialogueInput = KeyCode.Return;

    private bool _isStringBeingRevealed = false;
    private bool _isDialoguePlaying = false;
    private bool _isEndOfDialogue = false;

    public GameObject ContinueIcon;
    public GameObject StopIcon;
    public GameObject Panel;


    //Vector3 ThirdPersonController = GameObject.Find("ThirdPersonController").transform.position;
    public GameObject thirdpersoncontroller;



    // Use this for initialization
    void Start ()
    {
        _textComponent = GetComponent<Text>();
        _textComponent.text = "";
        Panel.SetActive(false);
        HideIcons();




    }

    // Update is called once per frame
    void Update () 
    {


        if (thirdpersoncontroller.transform.position.z > 37.6 && thirdpersoncontroller.transform.position.z < 38.8) {

            if (Input.GetKeyDown (KeyCode.Return)) {

                Panel.SetActive(true);

                if (!_isDialoguePlaying) {

                    _isDialoguePlaying = true;
                    StartCoroutine (StartDialogue ());

        }

    }

Solution

  • There are four possible reasons (with the solution)

    1. Your this condition is not true

      thirdpersoncontroller.transform.position.z > 37.6 && thirdpersoncontroller.transform.position.z < 38.8

    Log after the condition, if it is true and problem is still persist then,

    1. Check that your function (StartDialogue ()) is not deactivating your panel suddently. (if this is also not the case, then)

    2. is Any other script not making the panel deactivate? if it also not true

    3. Then, finally the problem is Panel is the child of an inactive GameObject. You can also check the Unity docs of SetActive

    Note that a GameObject may be inactive because a parent is not active.

    Note: These kind of questions are based on debugging (thats why i answer you in debugging manner) i will strongly recommend you to learn debugging skill in order to become a successful programmer.