0
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Player : MonoBehaviour
{
    [SerializeField] private DialogueUI dialogueUI;

    public Vector3 raypositionup = new Vector3(5, 0, 0);
    public Vector3 raypositiondown = new Vector3(5, 0, 0);

    private float MoveSpeed = 7f;

    public Animator animator;

    public DialogueUI DialogueUI => dialogueUI;

    public Interactable Interactable { get; set; }

    bool move = false;
    bool movefoward = false;

    public Rigidbody2D rb;

    Vector2 movement;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {

        if (DialogueUI.IsOpen) return;

        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        if (Input.GetKeyDown(KeyCode.W))
        {
            move = true;
            movefoward = false;
            animator.SetBool("Lookingfoward", true);
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            movefoward = true;
            move = false;
            animator.SetBool("Lookingfoward", false);
        }

        if (Input.GetKey(KeyCode.LeftShift))
        {
            MoveSpeed = 10f;
        }
        else
        {
            MoveSpeed = 7f;
        }

        if (Input.GetKeyDown(KeyCode.Return))
        {
            if (Interactable != null)
            {
                Interactable.Interact(this);
            }
        }

        if (move == true)
        {
            if (Input.GetKey(KeyCode.E))
            {

                Debug.DrawRay(transform.position + raypositionup, transform.up * 2f, Color.red);

                RaycastHit2D hit = Physics2D.Raycast(transform.position + raypositionup , transform.up, 2f);

                if (hit.collider.CompareTag("Object"))
                {
                    Debug.Log("Poop");
                    hit.transform.GetComponent<SpriteRenderer>().color = Color.red;
                }
                if (hit.collider == null)
                {
                    return;
                }
            }
        }

    }

    void FixedUpdate()
    {
        rb.MovePosition(rb.position + movement * MoveSpeed * Time.fixedDeltaTime);
    }
}

Ive tried several different fixes and none seem to work, such as another if statement, else statements, and even reworking the entire raycast system. If anyone knows a solution that would be just great. Also, any criticism on my code is okay and endorsed, im new to coding and want any opportunity I can get to clean up my code as best as possible.

Oxymoron
  • 19
  • 3

1 Answers1

1

You have code for return if collider is null, but not catch if null on compare:

if (hit.collider == null)
{
 return;
}
elseif (hit.collider.CompareTag("Object")) //No catched if null
{
 Debug.Log("Poop");
 hit.transform.GetComponent<SpriteRenderer>().color = Color.red;
}
joreldraw
  • 1,736
  • 1
  • 14
  • 28
  • Thank you! This fixed the problem, but I have another question, if I wanted to start an action if the raycast hit nothing, such as disabling another script ( which I am trying to do, as I only want my character to interact with an object they are looking at) how would I go about doing that? – Oxymoron Oct 28 '21 at 15:01
  • Hit nothing is impossible. If you like to do something when hit = null place your code on your "if" replacing the return. – joreldraw Oct 28 '21 at 15:22
  • if (hit.collider == null) { dialogueA.enabled = false; } – Oxymoron Oct 28 '21 at 15:37
  • I tried this, but got the same error as before. Is this what you meant or do I need to do something else? Thanks for the help! – Oxymoron Oct 28 '21 at 15:37
  • @Oxymoron I think what you mean is: **store** the last hit object in a field, and once it is not hit anymore (because either nothing or something else is hit) then invoke some method on it and then "forget" that reference and replace it by the next hit – derHugo Oct 28 '21 at 17:54