Search Unity

  1. Unity 2018.1 has arrived! Read about it here
    Dismiss Notice
  2. Scriptable Render Pipeline improvements, Texture Mipmap Streaming, and more! Check out what we have in store for you in the 2018.2 Beta.
    Dismiss Notice
  3. If you couldn't join the live stream, take a peek at what you missed.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Raycast causing confusing error

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

  1. JackMySon

    JackMySon

    Joined:
    Apr 15, 2018
    Posts:
    2
    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:
    1,080
    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:
    2
    Thanks for the reply, it worked perfectly! I’ll make sure to remember things like that in the future.