[C#] [2D] Weird bug with particles and linecasts. Need help!

Hello.
I have this really weird bug. It’s hard to explain so I made a video.

Here is my code:

using UnityEngine;
using System.Collections;
public class character : MonoBehaviour {

    public int health = 100;
    public bool isDead = false;
    public int XP = 0;
   
    public int hot = 0;
    public int cold = 0;
   
    public int coins = 0;

    public float speed = 4f;
    public float jumpHeight = 200f;

    public float chopTime = 4f;
    public float curChopTime;

    public Transform treeSight;
    public Transform jumpRay;
    public Transform spawnPoint;

    public RaycastHit2D nearTree;
    public RaycastHit2D onGround;

        // Color

    public enum curColor {
        white,
        orange,
        blue,
        green,
        red
    };
   
    public curColor color;

    void Update () {

            // Movement

        if (Input.GetKey (KeyCode.D)) {
            transform.Translate(Vector2.right * speed * Time.deltaTime);
        }

        if (Input.GetKey (KeyCode.A)) {
            transform.Translate(-Vector2.right * speed * Time.deltaTime);
        }

        if (Input.GetKey (KeyCode.RightArrow)) {
            transform.Translate(Vector2.right * speed * Time.deltaTime);
        }

        if (Input.GetKey (KeyCode.LeftArrow)) {
            transform.Translate(-Vector2.right * speed * Time.deltaTime);
        }

            // Tree chopping

        Debug.DrawLine(transform.position, treeSight.position, Color.black);
        nearTree = Physics2D.Linecast(transform.position, treeSight.position, 1 << LayerMask.NameToLayer("Tree"));

            if (Input.GetKey (KeyCode.E) && nearTree) {
                curChopTime += Time.deltaTime * 4;
                nearTree.transform.gameObject.GetComponentInChildren<ParticleSystem>().Play();
                    if (curChopTime >= chopTime) {
                        Destroy(nearTree.transform.gameObject);
                    }       
            } else {
                curChopTime = 0;
                nearTree.transform.gameObject.GetComponentInChildren<ParticleSystem>().Stop();
            }

            // Jumping

        Debug.DrawLine(transform.position, jumpRay.position, Color.green);
        onGround = Physics2D.Linecast(transform.position, jumpRay.position, 1 << LayerMask.NameToLayer("Ground"));

        if (Input.GetKeyDown (KeyCode.Space) && onGround) {
            GetComponent<Rigidbody2D>().AddForce (Vector2.up * jumpHeight);
        }


    }
}

Please, please, please help me. ;(

  • _zeph

Isn’t it possible for nearTree to be null at line 72? Then the jump code won’t be reached.

Edit: Rather it would be nearTree.transform that is null.

that should throw a null exception error and kill the game though?

No, it will not crash the game. It will only stop the execution of the current method. It won’t go past the line where the null exception occurs, hence it will never reach your jump code.

I don’t quite understand what should I do about the situation. Do you mind posting the script the way that it should be?

Instead of your current line 72, add this:

if(nearTree.transform != null) {
  nearTree.transform.gameObject.GetComponentInChildren<ParticleSystem>().Stop();
}