简体   繁体   中英

The type or namespace name could not be found Unity

I'm working on script for my fps game, but i'm still getting this error "The type or namespace name 'Target' could not be found." I know that there's something wrong with this line of code ReactiveTarget target = hitObject.GetComponent<Target>(); but i have no idea what it can't find this Target class

using UnityEngine;
using System.Collections

public class Shooting : MonoBehaviour{
private Camera _camera;

void Start(){
    _camera = GetComponent<Camera>();
    Cursor.lockState = CursorLockMode.Locked;
    Cursor.visible = false;
}

void OnGUI()
{
    int size = 12;
    float posX = _camera.pixelWidth / 2 - size / 4;
    float posY = _camera.pixelHeight / 2 - size / 2;
    GUI.Label(new Rect(posX, posY, size, size), "*");
}

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {

        Vector3 point = new Vector3(_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);

        Ray ray = _camera.ScreenPointToRay(point);

        RaycastHit hit;

        if (Physics.Raycast(ray, out hit))
        {
            GameObject hitObject = hit.transform.gameObject;

            ReactiveTarget target = hitObject.GetComponent<Target>();

            if (target != null)
            {
                target.ReactToHit();
            }
         
            else
            {
                StartCoroutine(SphereIndicator(hit.point));
            }
        }
    }
}
private IEnumerator SphereIndicator(Vector3 pos)
{
    GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    sphere.transform.position = pos;
    yield return new WaitForSeconds(1);
    Destroy(sphere);
}}

And here's the Target script

using UnityEngine;
using System.Collections;


public class Target : MonoBehaviour
{
    public void ReactToHit()
    {
        StartCoroutine(Die());
    }
    private IEnumerator Die()
    {
        this.transform.Rotate(-75, 0, 0);
        yield return new WaitForSeconds(1.5f);
        Destroy(this.gameObject);
    }
}

Hi

There is something wrong here(Or at least this is what I think)

How did you use the data type ReactiveTarget To get the data type of Target ?

This is the first time I see something like this!

Try to change

ReactiveTarget target = hitObject.GetComponent<Target>();

To:

Target target = hitObject.GetComponent<Target>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM