Top.Mail.Ru
Ответы
Аватар пользователя
Аватар пользователя
Аватар пользователя
Аватар пользователя
Программирование
+3

Не работает скрипт C#: error CS0246

Я новичок в C#, да и в целом в писании кода. Assets\Player\PickUpController.cs(8,11): error CS0246: The type or namespace name 'ProjectileItem' could not be found (are you missing a using directive or an assembly reference?)


Мой скрипт:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PickUpController : MonoBehaviour
{
public ProjectileItem itemScript;
public Rigidbody rb;
public BoxCollider coll;
public Transform player, itemContainer, fpsCam;

public float pickUpRange;
public float dropForwardForce, dropUpwardForce;

public bool equipped;
public static bool slotFull;

private void Start()
{
//Setup
if (!equipped)
{
itemScript.enabled = false;
rb.isKinematic = false;
coll.isTrigger = false;
}
if (!equipped)
{
itemScript.enabled = true;
rb.isKinematic = true;
coll.isTrigger = true;
slotFull = true;
}
}

private void Update()
{
//Check if player is in range and "E" is pressed
Vector3 distanceToPlayer = player.position - transform.position;
if (!equipped && distanceToPlayer.magnitude <= pickUpRange && Input.GetKey(KeyCode.E) && !slotFull) PickUp();

//Drop if equipped and "Q" is pressed
if (equipped && Input.GetKeyDown(KeyCode.Q)) Drop();
}



private void PickUp()
{
equipped = true;
slotFull = true;

//Make weapon a child of the camera and move it to default position
transform.SetParent(gunContainer);
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.Euler(Vector3.zero);
transform.localScale = Vector3.one;

//Make Rigidbody kinematic and BoxCollider a trigger
rb.isKinematic = true;
coll.isTrigger = true;

//Enable script
itemScript.enabled = true;
}

private void Drop()
{
equipped = false;
slotFull = false;

//Set parent to null
transform.SetParent(null);

//Make Rigidbody not kinematic and BoxCollider a normal
rb.isKinematic = false;
coll.isTrigger = false;

//Gun carries momentum of player
rb.velocity = player.GetComponent<Rigidbody>().velocity;

//AddForce
rb.AddForce(fpsCam.forward * dropForwardForce, ForceMode.Impulse);
rb.AddForce(fpsCam.up * dropUpwardForce, ForceMode.Impulse);
//Add random rotation
float random = Random.Range(-1f, 1f);
rb.AddTorque(new Vector3(random, random, random) * 10);

//disable script
itemScript.enabled = false;
}
}

По дате
По рейтингу
Аватар пользователя
Мастер
1
 public ProjectileItem itemScript 

Unity жалуется на эту строку, скорее всего ты либо имя класса неправильно написал, либо его вообще нету

Аватар пользователя
Искусственный Интеллект

У вас в проекте есть класс ProjectileItem?
Unity думает, что нет.

Аватар пользователя
Оракул

Вот пример исправленного кода:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
 using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using UnityEngine.UI; 
 
// Убедитесь, что пространство имен указано правильно, если необходимо 
// using MyGame.Items; 
 
public class PickUpController : MonoBehaviour 
{ 
    public ProjectileItem itemScript; // Убедитесь, что ProjectileItem существует и определен 
    public Rigidbody rb; 
    public BoxCollider coll; 
    public Transform player, itemContainer, fpsCam; 
 
    public float pickUpRange; 
    public float dropForwardForce, dropUpwardForce; 
 
    public bool equipped; 
    public static bool slotFull; 
 
    private void Start() 
    { 
        // Setup 
        if (!equipped) 
        { 
            itemScript.enabled = false; 
            rb.isKinematic = false; 
            coll.isTrigger = false; 
        } 
        else 
        { 
            itemScript.enabled = true; 
            rb.isKinematic = true; 
            coll.isTrigger = true; 
            slotFull = true; 
        } 
    } 
 
    private void Update() 
    { 
        // Check if player is in range and "E" is pressed 
        Vector3 distanceToPlayer = player.position - transform.position; 
        if (!equipped && distanceToPlayer.magnitude <= pickUpRange && Input.GetKey(KeyCode.E) && !slotFull) PickUp(); 
 
        // Drop if equipped and "Q" is pressed 
        if (equipped && Input.GetKeyDown(KeyCode.Q)) Drop(); 
    } 
 
    private void PickUp() 
    { 
        equipped = true; 
        slotFull = true; 
 
        // Make weapon a child of the camera and move it to default position 
        transform.SetParent(itemContainer); 
        transform.localPosition = Vector3.zero; 
        transform.localRotation = Quaternion.Euler(Vector3.zero); 
        transform.localScale = Vector3.one; 
 
        // Make Rigidbody kinematic and BoxCollider a trigger 
        rb.isKinematic = true; 
        coll.isTrigger = true; 
 
        // Enable script 
        itemScript.enabled = true; 
    } 
 
    private void Drop() 
    { 
        equipped = false; 
        slotFull = false; 
 
        // Set parent to null 
        transform.SetParent(null); 
 
        // Make Rigidbody not kinematic and BoxCollider normal 
        rb.isKinematic = false; 
        coll.isTrigger = false; 
 
        // Gun carries momentum of player 
        rb.velocity = player.GetComponent<Rigidbody>().velocity; 
 
        // AddForce 
        rb.AddForce(fpsCam.forward * dropForwardForce, ForceMode.Impulse); 
        rb.AddForce(fpsCam.up * dropUpwardForce, ForceMode.Impulse); 
        // Add random rotation 
        float random = Random.Range(-1f, 1f); 
        rb.AddTorque(new Vector3(random, random, random) * 10); 
 
        // Disable script 
        itemScript.enabled = false; 
    } 
}