Hi everyone,
i am facing again a problem i don’t understand.
What i want to achieve is if i hover with my mouse over a prefab (in this case a crate), i want that "Press “Use” is displayed next to the cursor (crosshair).
this works as long as i assign the UILabel per drag and drop to the “useLabel”.
but if i save my crate as a prefab i loose my connection to the UILabel.
(UILabel “useLabel” is on my playerprefab)
the script on my crate:
using UnityEngine;
using System.Collections;
public class Interact_2 : MonoBehaviour
{
public UILabel useLabel;
public bool canClick = false;
public float distanceToClick = 10.0f;
public enum State
{
open,
close,
inbetween
}
public State state;
void Start()
{
state = Interact_2.State.close;
useLabel.enabled = false;
}
void OnMouseOver ()
{
float sqrDistance= distanceToClick*distanceToClick;
Vector3 distance = Camera.main.transform.position - transform.position;
if(distance.sqrMagnitude<=sqrDistance)
{
canClick = true;
useLabel.enabled = true;
useLabel.text = "Press 'Use'";
if(Input.GetButtonDown("Use"))
{
switch(state)
{
case State.open:
state = Interact_2.State.inbetween;
StartCoroutine("Close");
break;
case State.close:
state = Interact_2.State.inbetween;
StartCoroutine("Open");
break;
}
}
}
else if (canClick)
{
canClick=false;
}
}
void OnMouseExit()
{
canClick = false;
useLabel.enabled = false;
}
private IEnumerator Open()
{
animation.Play("open_crate");
yield return new WaitForSeconds(animation["open_crate"].length);
Debug.Log("animation open played");
state = Interact_2.State.open;
}
private IEnumerator Close()
{
animation.Play("close_crate");
yield return new WaitForSeconds(animation["close_crate"].length);
Debug.Log("Animation close played");
state = Interact_2.State.close;
}
}
I am using NGUI, but i think it has nothing to do with NGUI, just with my incompetence of finding a solution.
Thanks