Following script is attached to every gameObject in scene that i would like to interact with.
When I look at one cube code is working, and when looking at the other one, it does ABSOLUTLY NOTHING! what have I done wrong? (I did not forget to attach the script to both gameobjects…)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CheckIfInteractable : MonoBehaviour {
public float interactionDistance = 2f;
Text descriptionText;
void Awake()
{
descriptionText = GameObject.Find ("DescriptionText").GetComponent<Text> ();
}
void Update()
{
if (Check (GetComponent<Collider>(),interactionDistance))
{
descriptionText.text = gameObject.name;
}
else
{
descriptionText.text = null;
}
}
private bool Check (Collider col, float interactionDistance)
{
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = Camera.main.ScreenPointToRay (new Vector3 (x, y));
RaycastHit hit;
if (col.Raycast (ray, out hit, interactionDistance))
return true;
return false;
}
}