Search Unity

isVisible() returning true and false?

Discussion in 'Scripting' started by Afropenguinn, Dec 19, 2014.

  1. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    I put the following into my character's update function:
    Code (CSharp):
    1. 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! :)
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    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?
     
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You running a quantum computer? In normal computers bools are either true or false. They can't return both equally.
     
    Oknaa and adrianccee like this.
  4. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    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.
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    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?
     
  6. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    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.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,532
    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:

    Code (csharp):
    1.  
    2. Debug.Log (transform.Find("Sprite/Body/_sprite").GetComponent<SpriteRenderer>().isVisible);
    3.  
    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.
     
  8. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    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):

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. [RequireComponent (typeof (BoxCollider2D))]
    6. [RequireComponent (typeof (Rigidbody2D))]
    7. public class Actor : CustomBehaviour
    8. {
    9.     //*****ACTOR VARIABLES*****//
    10.     //controller
    11.     public ActorController actorController
    12.     {
    13.         set
    14.         {
    15.             _actorController = value;
    16.             if (_actorController != null ) { _actorController.actor = this; }
    17.         }
    18.         get
    19.         {
    20.             return _actorController;
    21.         }
    22.     }
    23.     [SerializeField]
    24.     private ActorController _actorController;
    25.  
    26.     //personas
    27.     public ActorPersona activePersona
    28.     {
    29.         set
    30.         {
    31.             _activePersona = value;
    32.             if (_activePersona != null ) { _activePersona.actor = this; }
    33.         }
    34.         get
    35.         {
    36.             return _activePersona;
    37.         }
    38.     }
    39.     [SerializeField]
    40.     private ActorPersona _activePersona;
    41.  
    42.     public ActorPersona[] personas;
    43.  
    44.     //factions
    45.     public enum Faction { None, Player, Enemy }
    46.     public Faction faction = Faction.None;
    47.  
    48.     //spawn points
    49.     [HideInInspector]
    50.     public Transform RGunSpawn, LGunSpawn, RCastSpawn, LCastSpawn;
    51.  
    52.     //health
    53.     public float health = 100f;
    54.  
    55.     //movement variables
    56.     public float speed = 8f, turnSpeed = 360f;
    57.     [HideInInspector]
    58.     public float hForce = 0f, vForce = 0f, friction = 50f;
    59.  
    60.     public float direction
    61.     {
    62.         set
    63.         {
    64.             _direction = value;
    65.             if (_direction >= 360f) { _direction -= 360f; }
    66.             if (_direction < 0f) { _direction += 360f; }
    67.         }
    68.         get { return _direction; }
    69.     }
    70.     private float _direction;
    71.  
    72.     public float targetDirection
    73.     {
    74.         set
    75.         {
    76.             _targetDirection = value;
    77.             if (_targetDirection >= 360f) { _targetDirection -= 360f; }
    78.             if (_targetDirection < 0f) { _targetDirection += 360f; }
    79.         }
    80.         get { return _targetDirection; }
    81.     }
    82.     private float _targetDirection;
    83.  
    84.     public Vector2 virtualStick
    85.     {
    86.         set
    87.         {
    88.             _virtualStick = value;
    89.             if (_virtualStick.magnitude > 1f) { _virtualStick = _virtualStick.normalized; }
    90.         }
    91.         get { return _virtualStick; }
    92.     }
    93.     private Vector2 _virtualStick;
    94.  
    95.     //animation
    96.     Animator animator;
    97.     GameObject LLegPivot, RLegPivot;
    98.  
    99.     //*****INITIALIZATION******//
    100.     void Awake ()
    101.     {
    102.         //set up the rigidbody
    103.         rigidbody2D.gravityScale = 0f;
    104.         rigidbody2D.fixedAngle = true;
    105.         rigidbody2D.sleepMode = RigidbodySleepMode2D.NeverSleep;
    106.  
    107.         //set actor components
    108.         RGunSpawn = transform.FindChild("Sprite").FindChild("Body").FindChild("URArm").FindChild("LRArm").FindChild("RFist").FindChild("RGun").FindChild("RGunSpawn").transform;
    109.         LGunSpawn = transform;
    110.         RCastSpawn = transform;
    111.         LCastSpawn = transform;
    112.  
    113.         //animation variables
    114.         animator = GetComponent<Animator>();
    115.         LLegPivot = transform.FindChild("Sprite").FindChild("LLegPivot").gameObject;
    116.         RLegPivot = transform.FindChild("Sprite").FindChild("RLegPivot").gameObject;
    117.     }
    118.  
    119.     void OnEnable()
    120.     {
    121.         //set up controllers and personas
    122.         actorController = gameObject.GetComponent<ActorController>();
    123.         personas = gameObject.GetComponents<ActorPersona>();
    124.         if (personas.Length > 0) { SetPersona(personas[0]); }
    125.     }
    126.  
    127.     //*****ACTOR UPDATE******//
    128.     void FixedUpdate()
    129.     {
    130.         //turning
    131.         direction = Mathf.MoveTowardsAngle(direction, targetDirection, turnSpeed * Time.deltaTime);
    132.         rigidbody2D.rotation = direction;
    133.  
    134.         //animation
    135.         animator.SetFloat("speed", virtualStick.magnitude * speed);
    136.      
    137.         //leg animation
    138.         if (virtualStick == Vector2.zero) //not moving
    139.         {
    140.             LLegPivot.transform.localEulerAngles = new Vector3(0f, 0f, 0f); //set left leg
    141.             RLegPivot.transform.localEulerAngles = new Vector3(0f, 0f, 0f); //set right leg
    142.         }
    143.         else
    144.         {
    145.             float pivot = Functions2D.PointDirection(0f, 0f, virtualStick.x, virtualStick.y); //get the pivot direction
    146.             LLegPivot.transform.eulerAngles = new Vector3(0f, 0f, pivot); //set left leg
    147.             RLegPivot.transform.eulerAngles = new Vector3(0f, 0f, pivot); //set right leg
    148.  
    149.             if (RLegPivot.transform.localEulerAngles.z <= 270 && RLegPivot.transform.localEulerAngles.z >= 90f) //we are going in reverse
    150.             {
    151.                 LLegPivot.transform.eulerAngles = new Vector3(0f, 0f, pivot + 180f); //set left leg
    152.                 RLegPivot.transform.eulerAngles = new Vector3(0f, 0f, pivot + 180f); //set right leg
    153.             }
    154.         }
    155.     }
    156.  
    157.     void Update ()
    158.     {
    159.         //getting forces
    160.         Vector2 moveVector = virtualStick * (speed / 50f);
    161.         Vector2 forceVector = new Vector2(hForce / 50f, vForce / 50f);
    162.  
    163.         //friction of forces
    164.         if (hForce != 0)
    165.         {
    166.             if (hForce > 0)
    167.             {
    168.                 hForce -= friction * Time.deltaTime;
    169.                 if (moveVector.x >= hForce) { hForce = 0f; }
    170.             }
    171.             if (hForce < 0)
    172.             {
    173.                 hForce -= friction * Time.deltaTime;
    174.                 if (moveVector.x <= hForce) { hForce = 0f; }
    175.             }
    176.             if (hForce < friction * Time.deltaTime && hForce > -friction * Time.deltaTime) { hForce = 0f; }
    177.         }
    178.         if (vForce != 0)
    179.         {
    180.             if (vForce > 0)
    181.             {
    182.                 vForce -= friction * Time.deltaTime;
    183.                 if (moveVector.y >= vForce) { vForce = 0f; }
    184.             }
    185.             if (vForce < 0)
    186.             {
    187.                 vForce -= friction * Time.deltaTime;
    188.                 if (moveVector.y <= vForce) { vForce = 0f; }
    189.             }
    190.             if (vForce < friction * Time.deltaTime && vForce > -friction * Time.deltaTime) { vForce = 0f; }
    191.         }
    192.  
    193.         //movement
    194.         rigidbody2D.MovePosition(rigidbody2D.position + moveVector + forceVector);
    195.  
    196.         //check if it should be animated
    197.         if (Functions2D.PointDistance(transform.position.x, transform.position.y, Camera.main.transform.position.x, Camera.main.transform.position.y) >= 20f)
    198.         {
    199.             animator.enabled = false;
    200.         }
    201.         else
    202.         {
    203.             animator.enabled = true;
    204.         }
    205.  
    206. Debug.Log(transform.FindChild("Sprite").FindChild("Body").FindChild("_sprite").GetComponent<SpriteRenderer>().
    207.     }
    208.  
    209.     void LateUpdate()
    210.     {
    211.         //get controller input
    212.         if (actorController != null) { actorController.ControllerUpdate(); }
    213.     }
    214.  
    215.     //***** DESTROYED *****//
    216.     void OnDisable()
    217.     {
    218.         //destroy controller
    219.         ActorController[] actorControllers = GetComponents<ActorController>();
    220.         foreach(ActorController cont in actorControllers) { Destroy (cont); }
    221.         actorController = null;
    222.  
    223.         //destory personas
    224.         foreach(ActorPersona pers in personas) { Destroy (pers); }
    225.         activePersona = null;
    226.         personas = null;
    227.  
    228.         //set variables
    229.         faction = Faction.None;
    230.         direction = 0f;
    231.         targetDirection = 0f;
    232.         virtualStick = Vector2.zero;
    233.         speed = 8f;
    234.         turnSpeed = 360f;
    235.         hForce = 0f;
    236.         vForce = 0f;
    237.         health = 100f;
    238.  
    239.         //animation
    240.         //animator.enabled = false;
    241.     }
    242.  
    243.     //***** ACTOR FUNCTIONS *****//
    244.     //set controller
    245.     public void SetController(ActorController controller)
    246.     {
    247.         actorController = controller;
    248.  
    249.         //destroy other controllers
    250.         /*
    251.         ActorController[] temp  = gameObject.GetComponents<ActorController>();
    252.         if (temp.Length > 0)
    253.         {
    254.             for (int i=0; i<temp.Length; i+=1)
    255.             {
    256.                 if (temp[i] != controller) { Destroy(temp[i]); }
    257.             }
    258.         }
    259.         */
    260.     }
    261.  
    262.     //set persona
    263.     public void SetPersona(ActorPersona persona)
    264.     {
    265.         activePersona = persona;
    266.         ActorAbility[] temp  = gameObject.GetComponents<ActorAbility>();
    267.         if (temp.Length > 0)
    268.         {
    269.             for (int i=0; i<temp.Length; i+=1)
    270.             {
    271.                 Destroy(temp[i]);
    272.             }
    273.         }
    274.  
    275.         if (activePersona != null) { activePersona.Activate(); }
    276.     }
    277.  
    278.     //add controller
    279.     public void AddController(System.Type controller)
    280.     {
    281.         gameObject.AddComponent(controller);
    282.     }
    283.  
    284.     //add persona
    285.     public void AddPersona(System.Type persona)
    286.     {
    287.         gameObject.AddComponent(persona);
    288.         personas = gameObject.GetComponents<ActorPersona>();
    289.     }
    290.  
    291.     public void AddPersona(System.Type[] persona)
    292.     {
    293.         int i;
    294.         for (i=0; i<persona.Length; i+=1)
    295.         {
    296.             gameObject.AddComponent(persona[i]);
    297.         }
    298.         personas = gameObject.GetComponents<ActorPersona>();
    299.     }
    300.  
    301.     //use an ability
    302.     public void Use(int num, bool pressed)
    303.     {
    304.         if (pressed == true)
    305.         {
    306.             if (activePersona != null)
    307.             {
    308.                 if (num <=4 && num >= 1)
    309.                 {
    310.                     if (activePersona.abilities[num - 1] != null)
    311.                     {
    312.                         if (activePersona.abilities[num - 1].free == true || activePersona.abilities[num - 1].oneShot == false)
    313.                         {
    314.                             activePersona.abilities[num - 1].Use ();
    315.                             activePersona.abilities[num - 1].free = false;
    316.                         }
    317.                     }
    318.                     else
    319.                     {
    320.                         Debug.LogWarning("Ability missing in " + gameObject.name + ".");
    321.                     }
    322.                 }
    323.                 else
    324.                 {
    325.                     Debug.LogWarning(gameObject.name + " is trying to use a none-existant ability slot.");
    326.                 }
    327.             }
    328.             else
    329.             {
    330.                 Debug.LogWarning("Persona missing in " + gameObject.name + ".");
    331.             }
    332.         }
    333.         else
    334.         {
    335.             if (activePersona != null)
    336.             {
    337.                 if (num <=4 && num >= 1)
    338.                 {
    339.                     if (activePersona.abilities[num - 1] != null)
    340.                     {
    341.                         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."); }
    342.                     }
    343.                     else
    344.                     {
    345.                         Debug.LogWarning("Ability missing in " + gameObject.name + ".");
    346.                     }
    347.                 }
    348.                 else
    349.                 {
    350.                     Debug.LogWarning(gameObject.name + " is trying to free a none-existant ability slot.");
    351.                 }
    352.             }
    353.             else
    354.             {
    355.                 Debug.LogWarning("Persona missing in " + gameObject.name + ".");
    356.             }
    357.         }
    358.     }
    359.  
    360.     public void Use(int num)
    361.     {
    362.         Use (num, true);
    363.         Use (num, false);
    364.     }
    365.  
    366.     public void TakeDamage(float num)
    367.     {
    368.         health -= num;
    369.         if (health <= 0f) { Despawn(); }
    370.     }
    371.  
    372.     public void Despawn()
    373.     {
    374.         gameObject.SetActive(false);
    375.     }
    376.  
    377.     //***** EDITOR *****//
    378.     void OnValidate()
    379.     {
    380.         actorController = _actorController;
    381.         activePersona = _activePersona;
    382.     }
    383. }
    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!
     
    Last edited: Jan 6, 2015