Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Sprite Renderer won't render.

Discussion in 'Scripting' started by Moonbad, Dec 23, 2019.

  1. Moonbad

    Moonbad

    Joined:
    Dec 23, 2019
    Posts:
    2
    Hi, I've recently started learning how to use Unity. I already very familiar with the C# language and have no problems with it.

    I've created a Script for an Enemy that follows the Player around in a very simple manner and every time it collides with the player, it does a flashing effect that plays for half a second. This flashing effect is nothing more than a object with a white sprite that is connected right above the enemy, and the "animation" is just me messing with the sprite color. The problem is, this "animation" only renders on the scene view; inside the game it simply does not appear.

    This is the Script that I've created:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Enemy : MonoBehaviour
    4. {
    5.     public GameObject target;
    6.     private Vector3 direction;
    7.  
    8.     public GameObject hitfx;
    9.     private bool isHitfxActive = false;
    10.     private int hitfxCooldownTimer = 0;
    11.  
    12.     private void HitFX()
    13.     {
    14.         SpriteRenderer renderer = hitfx.GetComponent<SpriteRenderer>();
    15.  
    16.         if (isHitfxActive)
    17.         {
    18.             hitfxCooldownTimer++;
    19.             if (hitfxCooldownTimer >= 0 && hitfxCooldownTimer < 16)
    20.             {
    21.                 float pulse = hitfxCooldownTimer / 15f;
    22.                 renderer.color = new Color(1, 1, 1, pulse);
    23.             }
    24.          
    25.             if (hitfxCooldownTimer >= 16)
    26.             {
    27.                 float pulse = (30 - hitfxCooldownTimer) / 15f;
    28.                 renderer.color = new Color(1, 1, 1, pulse);
    29.             }
    30.          
    31.             if (hitfxCooldownTimer == 30)
    32.             {
    33.                 isHitfxActive = false;
    34.                 hitfxCooldownTimer = 0;
    35.             }
    36.         }
    37.     }
    38.  
    39.     void Update()
    40.     {
    41.         HitFX();
    42.  
    43.         direction = (target.transform.position - transform.position).normalized;
    44.         transform.Translate(direction * 4 / 60);
    45.     }
    46.  
    47.  
    48.     public void OnCollisionEnter2D(Collision2D collision)
    49.     {
    50.         isHitfxActive = true;
    51.     }
    52. }
    Can someone explain what I'm doing wrong?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Check that your white sprite is explicitly sorted above the regular sprite.
     
  3. Moonbad

    Moonbad

    Joined:
    Dec 23, 2019
    Posts:
    2
    Yes, I've checked it. The camera is at z = -10, the player and the enemy at z = 0, and the effect at z = -1.

    EDIT: I've also noticed that any new game object with a sprite renderer isn't showing up.

    EDIT: Well, it seems to be a problem with the z coordinate but the same time doesn't ...
     
    Last edited: Dec 23, 2019
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Keep in mind Z coordinate is ignored with sprites. It pays attention to sorting order and sorting layer.

    You can see this by putting two different sprites in scene, one Z-closer than the other, and set the close one to render sorting layer-wise below the upper one, then pan the camera around.

    The reason it looks different game vs scene wise is that those are different rendering paths and when the sorting order/layer is the same, the outcome is undefined.