Top.Mail.Ru
Ответы

Unity C# ошибка в коде программы

Кто знает где ошибка в этом скрипте?

1234567891011121314151617181920212223242526272829303132333435
 using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using TMPro; 
 
public class SelectionManager : MonoBehaviour 
{ 
public GameObject interaction_Info_UI; 
TMP_Text interaction_text; 
 
private void Start() 
{ 
    interaction_text = interaction_Info_UI.GetComponent<TMP_Text>(); 
} 
 
void Update() 
{ 
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
    RaycastHit hit; 
 
    if (Physics.Raycast(ray, out hit)) 
    { 
        var selectionTransform = hit.transform; 
        if (selectionTransform.GetComponent<InteractableObject>()) 
        { 
            interaction_text.text = selectionTransform.GetComponent<InteractableObject>().GetItemName(); 
            interaction_Info_UI.SetActive(true); 
        } 
        else 
        { 
            interaction_Info_UI.SetActive(false); 
        } 
    } 
} 
} 


Мне выводит ошибку -

1
 Assets\SCRIPTS\SelectionManager.cs(28,69): error CS0246: The type or namespace name 'InteractableObject' could not be found (are you missing a using directive or an assembly reference?) 
По дате
По Рейтингу
Аватар пользователя
Искусственный Интеллект
4мес

>Кто знает где ошибка в этом скрипте?
Ну я знаю. В 28-ой строке. Помогло?

Аватар пользователя
Мастер
4мес

может быть ты забыла поставить точку с запятой или скобки не закрыла проверь код

Аватар пользователя
Мастер
4мес

слушай если ошибка связана с переменной посмотри не забыл ли ты её объявить

Аватар пользователя
4мес

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

public class SelectionManager : MonoBehaviour
{
public GameObject interaction_Info_UI;
private TMP_Text interaction_text;
private InteractableObject currentInteractable;

private void Start()
{
interaction_text = interaction_Info_UI.GetComponent<TMP_Text>();
interaction_Info_UI.SetActive(false); // Изначально скрыть UI
}

void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;

if (Physics.Raycast(ray, out hit))
{
var selectionTransform = hit.transform;
InteractableObject interactable = selectionTransform.GetComponent<InteractableObject>();

if (interactable)
{
if (currentInteractable != interactable) // Проверка на изменения
{
currentInteractable = interactable;
interaction_text.text = currentInteractable.GetItemName();
interaction_Info_UI.SetActive(true);
}
}
else
{
if(currentInteractable != null)
{
currentInteractable = null;
interaction_Info_UI.SetActive(false);
}
}
}
else
{
if(currentInteractable != null)
{
currentInteractable = null;
interaction_Info_UI.SetActive(false);
}
}
}
}