[C#] saving a reference into a prefab

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

I had sort of the same issue with prefabs. Not sure if any info in here will help but it is worth a try.

http://forum.unity3d.com/threads/153056-Changing-a-value-in-a-prefab-before-it-Instantiates

Thanks, ALL_American.
Right after i posted my question i just added

GameObject _player = GameObject.FindGameObjectWithTag("Player");
		_label = _player.GetComponentInChildren<UILabel>();

in my Start function and it works, but i have no idea why^^
i have more labels in my GUI, so i don’t understand why it is choosing the right label…meh.
Still trying to understand how GetComponent and C# works :smile: