Search Unity

Raycast causing confusing error

Discussion in 'Physics' started by JackMySon, Jun 9, 2018.

  1. JackMySon

    JackMySon

    Joined:
    Apr 15, 2018
    Posts:
    13
    So I'm guessing the solution to my problem is something very simple, but I can't for the life of me figure it out.

    I have a fairly long and overly complex shooting system, I know, but I'm not very experienced and just wanted to quickly practice making a 2d game. The error I'm getting is on line 123 (bolded) and says there "object not set to an instance of an object". I've done a lot of testing and it only appears to happen when the raycast hits nothing, so I've tried putting all sorts of methods to prevent the error, but I still get one.

    This code here is what I use for shooting:


    IEnumerator ShotgunShoot()
    {
    isShoot = false;
    RaycastHit hit;
    Physics.Raycast(muzzle.transform.position, muzzle.transform.up, out hit, 10f);
    var bullet = Instantiate(bulletTracer, muzzle.transform.position, Quaternion.Euler(0, 0, (player.transform.localEulerAngles.z + 90)));
    bullet.transform.SetParent(player.transform, true);
    Destroy(bullet, 0.1f);
    rb.velocity = -muzzle.transform.up * knockback * Time.deltaTime;
    [B] if (hit.collider.gameObject.CompareTag("Player") && hit.collider.gameObject != player && hit.collider != null)[/B]
    {
    if (hit.collider.gameObject.GetComponent<Player1Move>().hasShield)
    {
    hit.collider.gameObject.GetComponent<Player1Move>().shield -= 20;
    }
    else
    {
    hit.collider.gameObject.GetComponent<Player1Move>().health -= 20;
    }

    StartCoroutine(hit.collider.gameObject.GetComponent<Player1Move>().ShowHealth());
    if (hit.collider.gameObject.GetComponent<Player1Move>().health <= 0)
    {
    kills++;
    GameManager.playersLeftAlive.Remove(hit.collider.gameObject);
    }
    }
    else if (hit.collider.gameObject.tag != "Player")
    {
    hitEffects.transform.position = hit.point;
    hitEffects.GetComponent<ParticleSystem>().Play();
    }
    else
    {
    isShoot = true;
    yield break;
    }

    yield return new WaitForSeconds(0.5f);
    isShoot = true;
    }


    If anyone has any questions about what any of the variables, etc. are, feel free to ask.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Boolean operations are executed in a specific order. Several AND sentences (&&) are evaluated in the order they are written. When "hit.collider" is null, the first sentence will try to access the gameObject of a null object, hence the error.

    Just rewrite the line like this:
    Code (CSharp):
    1. if (hit.collider != null && hit.collider.gameObject.CompareTag("Player") && hit.collider.gameObject != player)
    First, "hit.collider != null" will be evaluated. If the result is false, then the other sentences in the AND sequence won't be evaluated.

    Note that this is how AND operations work. In an OR operation, all sentences are evaluated in order until one results true. I'd recommend you to learn how to use boolean operators.
     
  3. JackMySon

    JackMySon

    Joined:
    Apr 15, 2018
    Posts:
    13
    Thanks for the reply, it worked perfectly! I’ll make sure to remember things like that in the future.