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. Dismiss Notice

Resolved [2D Top Down Game] Trying to make the tree & bush sprites fade when the player is behind it

Discussion in 'Scripting' started by SpiderPig1660, Feb 16, 2021.

?

Is it better to use tilemap or manually place sprites?

  1. Tilemap!!!!!

  2. Just yeet all the sprites

Results are only viewable after voting.
  1. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    Errors: NullReferenceException: Object reference not set to an instance of an object
    PlayerMovement.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/Player/PlayerScripts/PlayerMovement.cs:77)
    NullReferenceException: Object reference not set to an instance of an object PlayerMovement.OnTriggerExit2D (UnityEngine.Collider2D collision) (at Assets/Player/PlayerScripts/PlayerMovement.cs:86)

    Player Script:
    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         if (collision.tag == "Environment")
    4.         {
    5.             EnvironmentTransparent Script = collision.GetComponent<EnvironmentTransparent>();
    6.             Debug.Log("Bruh");
    7.             Script.FadeOut();
    8.         }
    9.     }
    10.  
    11.     private void OnTriggerExit2D(Collider2D collision)
    12.     {
    13.         if (collision.tag == "Environment")
    14.         {
    15.             EnvironmentTransparent Script = collision.GetComponent<EnvironmentTransparent>();
    16.             Script.FadeIn();
    17.         }
    18.     }
    Sprite fade script:
    Code (CSharp):
    1. public class EnvironmentTransparent : MonoBehaviour
    2. {
    3.  
    4.     public SpriteRenderer sprite;
    5.     private Color defaultcolor;
    6.     private Color fadedcolor;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         sprite = GetComponent<SpriteRenderer>();
    12.  
    13.         defaultcolor = sprite.color;
    14.         fadedcolor = defaultcolor;
    15.         fadedcolor.a = 0.7f;
    16.     }
    17.    
    18.     public void FadeIn()
    19.     {
    20.         sprite.color = defaultcolor;
    21.     }
    22.  
    23.     public void FadeOut()
    24.     {
    25.         sprite.color = fadedcolor;
    26.     }
    27. }
    28.  
    Thanks to anyone willing to help me :D
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    The answer is always the same... ALWAYS. It is the single most common error ever. Don't waste your life on this problem. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
    SpiderPig1660 likes this.
  3. SpiderPig1660

    SpiderPig1660

    Joined:
    Jul 26, 2020
    Posts:
    37
    OMG, I'm so dumb... I forgot to add the script to the actual gameobject :p