isVisible() returning true and false?

I put the following into my character’s update function:

Debug.Log (transform.FindChild("Sprite").FindChild("Body").FindChild("_sprite").GetComponent<SpriteRenderer>().isVisible);

What it returns is both true and false equally no matter the circumstances. Would anyone know why that is? Also I understand this is not optimal, I just threw it in for testing purposes.

Thanks for reading Unity Community! :slight_smile:

How do you mean it is both true and false equally in any circumstance? That is not possible. What message are getting thats making you say that?

1 Like

You running a quantum computer? In normal computers bools are either true or false. They can’t return both equally.

2 Likes

Sorry I guess I stated it wrong, I meant that it returned true and false alternating in rapid succession. So when I did a debug to see what it was returning it showed me an equal number for true and false that is constantly going up.

Do you by chance have this script attached to 2 different objects at the same time? One to an object that is visible, the other to one not visible?

Nope, I made sure there was only one object when I was testing it.
EDIT: Just as a note I am using 2D sprites in a perspective camera if that would cause any problems.

Also, you do know that ‘FindChild’ is considered deprecated, you should be using ‘transform.Find’, and that you can pass in a ‘/’ delimited string to find children of children:

Debug.Log (transform.Find("Sprite/Body/_sprite").GetComponent<SpriteRenderer>().isVisible);

As for your problem. This code shouldn’t be returning true, false, true, false, alternating, unless there is some circumstance outside of this code causing it. Something that we have no information about since all we have is the code you shared.

I’m not going to sit here and list off ideas of possibilities (of which there are many), and have you just say “nope, not it”, like a game of hangman. Look into your scene more, give us more information, something.

Ah I didn’t realize that FindChild was outdated. I also deeply apologize for wasting your time like that, here is the entire script for the character (I know it is a mess, this is my first full game):

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

[RequireComponent (typeof (BoxCollider2D))]
[RequireComponent (typeof (Rigidbody2D))]
public class Actor : CustomBehaviour
{
    //*****ACTOR VARIABLES*****//
    //controller
    public ActorController actorController
    {
        set
        {
            _actorController = value;
            if (_actorController != null ) { _actorController.actor = this; }
        }
        get
        {
            return _actorController;
        }
    }
    [SerializeField]
    private ActorController _actorController;

    //personas
    public ActorPersona activePersona
    {
        set
        {
            _activePersona = value;
            if (_activePersona != null ) { _activePersona.actor = this; }
        }
        get
        {
            return _activePersona;
        }
    }
    [SerializeField]
    private ActorPersona _activePersona;

    public ActorPersona[] personas;

    //factions
    public enum Faction { None, Player, Enemy }
    public Faction faction = Faction.None;

    //spawn points
    [HideInInspector]
    public Transform RGunSpawn, LGunSpawn, RCastSpawn, LCastSpawn;

    //health
    public float health = 100f;

    //movement variables
    public float speed = 8f, turnSpeed = 360f;
    [HideInInspector]
    public float hForce = 0f, vForce = 0f, friction = 50f;

    public float direction
    {
        set
        {
            _direction = value;
            if (_direction >= 360f) { _direction -= 360f; }
            if (_direction < 0f) { _direction += 360f; }
        }
        get { return _direction; }
    }
    private float _direction;

    public float targetDirection
    {
        set
        {
            _targetDirection = value;
            if (_targetDirection >= 360f) { _targetDirection -= 360f; }
            if (_targetDirection < 0f) { _targetDirection += 360f; }
        }
        get { return _targetDirection; }
    }
    private float _targetDirection;

    public Vector2 virtualStick
    {
        set
        {
            _virtualStick = value;
            if (_virtualStick.magnitude > 1f) { _virtualStick = _virtualStick.normalized; }
        }
        get { return _virtualStick; }
    }
    private Vector2 _virtualStick;

    //animation
    Animator animator;
    GameObject LLegPivot, RLegPivot;

    //*****INITIALIZATION******//
    void Awake ()
    {
        //set up the rigidbody
        rigidbody2D.gravityScale = 0f;
        rigidbody2D.fixedAngle = true;
        rigidbody2D.sleepMode = RigidbodySleepMode2D.NeverSleep;

        //set actor components
        RGunSpawn = transform.FindChild("Sprite").FindChild("Body").FindChild("URArm").FindChild("LRArm").FindChild("RFist").FindChild("RGun").FindChild("RGunSpawn").transform;
        LGunSpawn = transform;
        RCastSpawn = transform;
        LCastSpawn = transform;

        //animation variables
        animator = GetComponent<Animator>();
        LLegPivot = transform.FindChild("Sprite").FindChild("LLegPivot").gameObject;
        RLegPivot = transform.FindChild("Sprite").FindChild("RLegPivot").gameObject;
    }

    void OnEnable()
    {
        //set up controllers and personas
        actorController = gameObject.GetComponent<ActorController>();
        personas = gameObject.GetComponents<ActorPersona>();
        if (personas.Length > 0) { SetPersona(personas[0]); }
    }
 
    //*****ACTOR UPDATE******//
    void FixedUpdate()
    {
        //turning
        direction = Mathf.MoveTowardsAngle(direction, targetDirection, turnSpeed * Time.deltaTime);
        rigidbody2D.rotation = direction;

        //animation
        animator.SetFloat("speed", virtualStick.magnitude * speed);
     
        //leg animation
        if (virtualStick == Vector2.zero) //not moving
        {
            LLegPivot.transform.localEulerAngles = new Vector3(0f, 0f, 0f); //set left leg
            RLegPivot.transform.localEulerAngles = new Vector3(0f, 0f, 0f); //set right leg
        }
        else
        {
            float pivot = Functions2D.PointDirection(0f, 0f, virtualStick.x, virtualStick.y); //get the pivot direction
            LLegPivot.transform.eulerAngles = new Vector3(0f, 0f, pivot); //set left leg
            RLegPivot.transform.eulerAngles = new Vector3(0f, 0f, pivot); //set right leg

            if (RLegPivot.transform.localEulerAngles.z <= 270 && RLegPivot.transform.localEulerAngles.z >= 90f) //we are going in reverse
            {
                LLegPivot.transform.eulerAngles = new Vector3(0f, 0f, pivot + 180f); //set left leg
                RLegPivot.transform.eulerAngles = new Vector3(0f, 0f, pivot + 180f); //set right leg
            }
        }
    }

    void Update ()
    {
        //getting forces
        Vector2 moveVector = virtualStick * (speed / 50f);
        Vector2 forceVector = new Vector2(hForce / 50f, vForce / 50f);

        //friction of forces
        if (hForce != 0)
        {
            if (hForce > 0)
            {
                hForce -= friction * Time.deltaTime;
                if (moveVector.x >= hForce) { hForce = 0f; }
            }
            if (hForce < 0)
            {
                hForce -= friction * Time.deltaTime;
                if (moveVector.x <= hForce) { hForce = 0f; }
            }
            if (hForce < friction * Time.deltaTime && hForce > -friction * Time.deltaTime) { hForce = 0f; }
        }
        if (vForce != 0)
        {
            if (vForce > 0)
            {
                vForce -= friction * Time.deltaTime;
                if (moveVector.y >= vForce) { vForce = 0f; }
            }
            if (vForce < 0)
            {
                vForce -= friction * Time.deltaTime;
                if (moveVector.y <= vForce) { vForce = 0f; }
            }
            if (vForce < friction * Time.deltaTime && vForce > -friction * Time.deltaTime) { vForce = 0f; }
        }

        //movement
        rigidbody2D.MovePosition(rigidbody2D.position + moveVector + forceVector);

        //check if it should be animated
        if (Functions2D.PointDistance(transform.position.x, transform.position.y, Camera.main.transform.position.x, Camera.main.transform.position.y) >= 20f)
        {
            animator.enabled = false;
        }
        else
        {
            animator.enabled = true;
        }

Debug.Log(transform.FindChild("Sprite").FindChild("Body").FindChild("_sprite").GetComponent<SpriteRenderer>().
    }

    void LateUpdate()
    {
        //get controller input
        if (actorController != null) { actorController.ControllerUpdate(); }
    }

    //***** DESTROYED *****//
    void OnDisable()
    {
        //destroy controller
        ActorController[] actorControllers = GetComponents<ActorController>();
        foreach(ActorController cont in actorControllers) { Destroy (cont); }
        actorController = null;

        //destory personas
        foreach(ActorPersona pers in personas) { Destroy (pers); }
        activePersona = null;
        personas = null;

        //set variables
        faction = Faction.None;
        direction = 0f;
        targetDirection = 0f;
        virtualStick = Vector2.zero;
        speed = 8f;
        turnSpeed = 360f;
        hForce = 0f;
        vForce = 0f;
        health = 100f;

        //animation
        //animator.enabled = false;
    }

    //***** ACTOR FUNCTIONS *****//
    //set controller
    public void SetController(ActorController controller)
    {
        actorController = controller;

        //destroy other controllers
        /*
        ActorController[] temp  = gameObject.GetComponents<ActorController>();
        if (temp.Length > 0)
        {
            for (int i=0; i<temp.Length; i+=1)
            {
                if (temp[i] != controller) { Destroy(temp[i]); }
            }
        }
        */
    }

    //set persona
    public void SetPersona(ActorPersona persona)
    {
        activePersona = persona;
        ActorAbility[] temp  = gameObject.GetComponents<ActorAbility>();
        if (temp.Length > 0)
        {
            for (int i=0; i<temp.Length; i+=1)
            {
                Destroy(temp[i]);
            }
        }

        if (activePersona != null) { activePersona.Activate(); }
    }

    //add controller
    public void AddController(System.Type controller)
    {
        gameObject.AddComponent(controller);
    }

    //add persona
    public void AddPersona(System.Type persona)
    {
        gameObject.AddComponent(persona);
        personas = gameObject.GetComponents<ActorPersona>();
    }

    public void AddPersona(System.Type[] persona)
    {
        int i;
        for (i=0; i<persona.Length; i+=1)
        {
            gameObject.AddComponent(persona[i]);
        }
        personas = gameObject.GetComponents<ActorPersona>();
    }

    //use an ability
    public void Use(int num, bool pressed)
    {
        if (pressed == true)
        {
            if (activePersona != null)
            {
                if (num <=4 && num >= 1)
                {
                    if (activePersona.abilities[num - 1] != null)
                    {
                        if (activePersona.abilities[num - 1].free == true || activePersona.abilities[num - 1].oneShot == false)
                        {
                            activePersona.abilities[num - 1].Use ();
                            activePersona.abilities[num - 1].free = false;
                        }
                    }
                    else
                    {
                        Debug.LogWarning("Ability missing in " + gameObject.name + ".");
                    }
                }
                else
                {
                    Debug.LogWarning(gameObject.name + " is trying to use a none-existant ability slot.");
                }
            }
            else
            {
                Debug.LogWarning("Persona missing in " + gameObject.name + ".");
            }
        }
        else
        {
            if (activePersona != null)
            {
                if (num <=4 && num >= 1)
                {
                    if (activePersona.abilities[num - 1] != null)
                    {
                        if (activePersona.abilities[num - 1].free == false) { activePersona.abilities[num - 1].free = true; } else { Debug.LogWarning(gameObject.name + " is trying to free a none pressed ability."); }
                    }
                    else
                    {
                        Debug.LogWarning("Ability missing in " + gameObject.name + ".");
                    }
                }
                else
                {
                    Debug.LogWarning(gameObject.name + " is trying to free a none-existant ability slot.");
                }
            }
            else
            {
                Debug.LogWarning("Persona missing in " + gameObject.name + ".");
            }
        }
    }

    public void Use(int num)
    {
        Use (num, true);
        Use (num, false);
    }

    public void TakeDamage(float num)
    {
        health -= num;
        if (health <= 0f) { Despawn(); }
    }

    public void Despawn()
    {
        gameObject.SetActive(false);
    }

    //***** EDITOR *****//
    void OnValidate()
    {
        actorController = _actorController;
        activePersona = _activePersona;
    }
}

It is a lot to go through though. And to shed some more light on things: It Is a top down shooter where everything is 3D except the characters. The characters are layered 2D sprites (layered in 3D) to make them pop out more. As such I use a perspective camera. Hope this helps! If there is anything else you need let me know, and I understand if no one wants to take the time to go through all of this. I appreciate your time regardless, thank you!