Answer the question
In order to leave comments, you need to log in
Error in Unity related to GameObject array, how to solve it?
There is a project on the nose, my task was to texturize the models and create a scene and a "game" in Unity based on the models made for me. Right to the point, I need to make sure that the game object is included (in my case, pictures with text are sort of hints), depending on the tag of the object from the array.
I wrote the code, after which I edited it many times and achieved what you see below, but the problem is that an error
appeared
.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIManager : MonoBehaviour
{
[SerializeField] private Camera fpsCamera;
private Ray ray;
private RaycastHit hit;
[SerializeField] private float maxDistRay;
public GameObject Osc;
public GameObject Tisk;
public GameObject Yashik;
public GameObject RuPod;
public GameObject Car;
public GameObject Pod;
public GameObject Gaz;
public GameObject Verst;
public GameObject Anal;
public GameObject Ins;
public GameObject[] boba;
private void Update()
{
Ray();
DrawRay();
Interact();
}
private void Ray()
{
ray = fpsCamera.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
}
private void DrawRay()
{
if (Physics.Raycast(ray, out hit, maxDistRay));
{
Debug.DrawRay(ray.origin, ray.direction * maxDistRay, Color.blue);
}
if (hit.transform == null)
{
Debug.DrawRay(ray.origin, ray.direction * maxDistRay, Color.red);
}
}
private void Interact()
{
if (hit.transform != null && hit.transform.GetComponent<Rigidbody>())
{
Debug.DrawRay(ray.origin, ray.direction * maxDistRay, Color.green);
if (Input.GetKeyDown(KeyCode.E))
{
if (boba.gameObject.tag == ("Osc"))
{
Osc.SetActive(true);
}
if (boba.gameObject.tag == ("Tisk"))
{
Tisk.SetActive(true);
}
}
}
else
{
Osc.SetActive(false);
Tisk.SetActive(false);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question