Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# renderer.enabled isn't working ?

Discussion in 'Scripting' started by clearrose, Sep 29, 2015.

  1. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    I was trying to re-enable my sprite renders after a certain amount of time after being "eaten" by the player. Because, after the objects return to our pool and re-spawn they are still disabled. Looking like they were already "eaten".The weird thing is that I was calling GetComponent<Renderer>(); in on trigger enter and that was working fine. But, when I try to write a new void (that would be called after the on trigger enter) to enable and turn back on the sprites. Unity won't autocorrect and said that it didn't contain a definition for enabled? How can we fix this? any workarounds?

    Code (CSharp):
    1.   //Called when the player enters a triggerer zone
    2.     public void OnTriggerEnter2D(Collider2D other)
    3.     {
    4.         //If the submarine is collided with a coin
    5.         if (other.tag == "Coin")
    6.         {
    7.             //Notify the level manager, and disable the coin's renderer and collider
    8.             levelManager.CoinCollected(other.transform.position);
    9.             other.GetComponent<Renderer>().enabled = false;
    10.             other.GetComponent<Collider2D>().enabled = false;
    11.  
    12.             AudioManager.Instance.PlayCoinCollected();
    13.             Invoke("SquishyReappear",7);
    14.         }
    15.         //If the submarine is collided with an obstacle
    16.         else if (other.tag == "Obstacle")
    17.         {
    18.             //Notify the level manager, and disable the obstacle's renderer and collider
    19.             levelManager.Collision(other.name, other.transform.position);
    20.             //other.GetComponent<Renderer>().enabled = false;
    21.             other.GetComponent<Collider2D>().enabled = false;
    22.             GetComponent<Animator> ().Play ("TurtleCrash");
    23.  
    24.             AudioManager.Instance.PlayExplosion();
    25.  
    26.             //If the obstacle is a torpedo, disable it's child as well
    27.             if (other.name == "Torpedo")
    28.                 other.transform.FindChild("TorpedoFire").gameObject.SetActive(false);
    29.             if (other.name == "FrontShark")
    30.                 GetComponent<Animator> ().Play ("TurtleCrash");
    31.  
    32.             //If the player is vulnerable
    33.             if (playerVulnerability == PlayerVulnerability.Enabled)
    34.             {
    35.                 //Sink the submarine
    36.                 powerupUsage = PowerupUsage.Disabled;
    37.                 playerVulnerability = PlayerVulnerability.Disabled;
    38.                 playerStatus = PlayerStatus.Sinking;
    39.  
    40.                 GetComponent<Animator> ().Play ("TurtleCrash");
    41.                 //BackgroundStop.enabled =false;
    42.                 BackgroundStop.speed = 0.0f;
    43.                 //Waterline.enabled = false;
    44.                 Waterline.speed = 0.0f;
    45.                 Waterline2.speed = 0.0f;
    46.                 Turtle.isKinematic = true;
    47.                 normalCollider.enabled = false;
    48.                 GetComponent<BoxCollider2D>().enabled = false;
    49.                 //Debug.Log ("Turtle is now Kinematic");
    50.                 TurtleBackgroundOffset2.enabled = false;
    51.                 IsDead = true;
    52.                 //Debug.Log("player is dead");
    53.                 subRenderer.sprite = subTextures[currentSkinID * 2];
    54.                 bubbles.Stop();
    55.             }
    56.             //If the player is shielded, collapse it
    57.             else if (playerVulnerability == PlayerVulnerability.Shielded)
    58.             {
    59.                 CollapseShield();
    60.            
    61.             }
    62.         }
    63.         //If the submarine is collided with a powerup
    64.         else if (other.tag == "Powerup")
    65.         {
    66.             //Notify the level manager, and disable the powerup's components
    67.             other.GetComponent<Renderer>().enabled = false;
    68.             other.GetComponent<Collider2D>().enabled = false;
    69.             other.transform.FindChild("Trail").gameObject.SetActive(false);
    70.  
    71.             AudioManager.Instance.PlayPowerupCollected();
    72.             levelManager.PowerupPickup(other.name);
    73.         }
    74.     }
    75.  
    76.     public void SquishyReappear()
    77.     {
    78.         Squishy = GameObject.FindGameObjectsWithTag ("Coin");
    79.  
    80.         Squishy.GetComponents<Renderer>().enabled = true;
    81.         Squishy.GetComponents<Collider2D>().enabled = true;
    82.     }
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You're using 'GetComponents', the plural version. You get an array and forgot to access a specific element.
    Either loop through them if you have several components of the type on the objects, or directly access one using an index ( [0] for example). Or, if you're sure you'll only have one of those components each, just use GetComponent which returns a single component.
     
  3. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Ok I got the array to work, but I used set active instead of GetComponent. I figured that should reset all the comments on the game objects.

    However now, there is a new problem. Since many objects share the tag "coins" it is resetting all the objects tagged as so, on and off together. Including the objects that have just spawned and are still onscreen.

    Updated Code:

    Code (CSharp):
    1.  //Called when the player enters a triggerer zone
    2.     public void OnTriggerEnter2D(Collider2D other)
    3.     {
    4.         //If the submarine is collided with a coin
    5.         if (other.tag == "Coin")
    6.         {
    7.             //Notify the level manager, and disable the coin's renderer and collider
    8.             levelManager.CoinCollected(other.transform.position);
    9.             other.GetComponent<Renderer>().enabled = false;
    10.             other.GetComponent<Collider2D>().enabled = false;
    11.  
    12.             Invoke("SquishyReappear",20.0f);
    13.  
    14.             //other.GetComponent<Renderer>().enabled = true;
    15.             //other.GetComponent<Collider2D>().enabled = true;
    16.  
    17.             AudioManager.Instance.PlayCoinCollected();
    18.         }
    Code (CSharp):
    1. public void SquishyReappear()
    2.     {
    3.         StartCoroutine(SquishyReappearNow());
    4.     }
    Code (CSharp):
    1. IEnumerator SquishyReappearNow ()
    2.     {
    3.         Squishy = GameObject.FindGameObjectsWithTag ("Coin");
    4.        
    5.         foreach (GameObject _obj in Squishy)
    6.         {
    7.             _obj.SetActive(false);
    8.  
    9.             Debug.Log("Squishies is set to false");
    10.         }
    11.  
    12.         yield return new WaitForSeconds (5.0f);
    13.  
    14.         Squishy = GameObject.FindGameObjectsWithTag ("Coin");
    15.        
    16.         foreach (GameObject _obj in Squishy)
    17.         {
    18.             _obj.SetActive(true);
    19.  
    20.             Debug.Log("Squishies is set to true");
    21.         }
    Hope, you can help, and thanks for the continued support.
     
  4. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Skip the invoke and start coroutine directly. Put the delay in the coroutine. pass the coin as an argument to the routine and use this reference instead of finding all objects tagged coin. you could also offload this logic to a component on the coin itself.
     
  5. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    How would you approach this in code? I'm still learning.
     
  6. clearrose

    clearrose

    Joined:
    Oct 24, 2012
    Posts:
    349
    Thanks, we got it to work by offloading them just as you suggested.