I have an error with my code.

I am a beginner at Unity and I am having an error with my code.

Assets\movement.cs(44,33): error CS1503: Argument 1: cannot convert from ‘UnityEngine.GameObject’ to ‘string’

movement Script:

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

public class movement : MonoBehaviour
{
    float hit1distance;
    float hit2distance;
    float hit3distance;
    float hit4distance;
    float hit5distance;
    float hit6distance;
    float hit7distance;
    float hit8distance;

    GameObject Path1;
    GameObject Path2;
    GameObject Path3;
    GameObject Path4;
    GameObject Path5;
    GameObject Path6;
    GameObject Path7;
    GameObject Path8;

    void Start()
    {
       
    }
    void Update()
    {

    }
    void FixedUpdate()
    {
        RaycastHit2D hit1 = Physics2D.Raycast(transform.position, transform.position + new Vector3(0f, 1f, 0f));
        RaycastHit2D hit2 = Physics2D.Raycast(transform.position, transform.position + new Vector3(1f, 1f, 0f));
        RaycastHit2D hit3 = Physics2D.Raycast(transform.position, transform.position + new Vector3(1f, 0f, 0f));
        RaycastHit2D hit4 = Physics2D.Raycast(transform.position, transform.position + new Vector3(1f, -1f, 0f));
        RaycastHit2D hit5 = Physics2D.Raycast(transform.position, transform.position + new Vector3(0f, -1f, 0f));
        RaycastHit2D hit6 = Physics2D.Raycast(transform.position, transform.position + new Vector3(-1f, 1f, 0f));
        RaycastHit2D hit7 = Physics2D.Raycast(transform.position, transform.position + new Vector3(-1f, 0f, 0f));
        RaycastHit2D hit8 = Physics2D.Raycast(transform.position, transform.position + new Vector3(-1f, -1f, 0f));

        Path1 = GameObject.Find(hit1.transform.gameObject);
        if (Path1.tag == "Obstacle")
        {
            hit1distance = hit1.distance;
        }
        Path2 = GameObject.Find(hit2.transform.gameObject);
        if (Path2.tag == "Obstacle")
        {
            hit2distance = hit2.distance;
        }
        Path3 = GameObject.Find(hit3.transform.gameObject);
        if (Path3.tag == "Obstacle")
        {
            hit3distance = hit3.distance;
        }
        Path4 = GameObject.Find(hit4.transform.gameObject);
        if (Path4.tag == "Obstacle")
        {
            hit4distance = hit4.distance;
        }
        Path5 = GameObject.Find(hit5.transform.gameObject);
        if (Path5.tag == "Obstacle")
        {
            hit5distance = hit5.distance;
        }
        Path6 = GameObject.Find(hit6.transform.gameObject);
        if (Path6.tag == "Obstacle")
        {
            hit6distance = hit6.distance;
        }
        Path7 = GameObject.Find(hit7.transform.gameObject);
        if (Path7.tag == "Obstacle")
        {
            hit7distance = hit7.distance;
        }
        Path8 = GameObject.Find(hit8.transform.gameObject);
        if (Path8.tag == "Obstacle")
        {
            hit8distance = hit8.distance;
        }

        Debug.Log(Mathf.Min(hit1distance, hit2distance, hit3distance, hit4distance, hit5distance, hit6distance, hit7distance, hit8distance));
    }
}

GameObject.Find expects a string, not a gameobject. However, using that call in FixedUpdate will be terrible for performance and really I’m not sure why you are trying to use .Find anyways when the RaycastHit2D already will return something it hits.

I’d suggest looking into RaycastHit2D examples.

1 Like

How to understand compiler and other errors and even fix them yourself:

https://discussions.unity.com/t/824586/8

How to report your problem productively in the Unity3D forums:

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