Gui dont display item name

I have problem. I maked script and it doesnt work! Here is error:

A field initializer cannot reference the nonstatic field, method, or property `CollectedItems.inam’

And code for collecting items

using UnityEngine;
using System.Collections;

public class AnotherCollectables : MonoBehaviour {



	void OnTriggerEnter2D(Collider2D collider)
	{
		if(collider.gameObject.tag == "player")
			collider.GetComponent<CollectedItems>().inam = this.gameObject;
		
		Destroy (this.gameObject);
	
	}
	
}

and code for collected items
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CollectedItems : MonoBehaviour {
	
	public GameObject inam;

	private GameObject item = inam;
	
	void OnGUI()
	{
		if(item == inam)
		{ string itmTxt = "Zebrano : --- "; }
		else { string itmTxt = "Zebrano : " + item.transform.name; }

		GUI.Box (new Rect (Screen.width / 2, Screen.height / 2, 150, 150), itmTxt);
	}
}

Your problem is here:

 public GameObject inam;
 
 private GameObject item = inam;

You cannot initialize a field based on a property, you have to do that in the constructor, or in the case of MonoBehaviour you can do it in start:

public GameObject inam;
 
private GameObject item;

void Start() 
{
     item = inam;
}

collider.GetComponent().inam = this.gameObject;

replace with:

collider.GetComponent().SendMessage(“Inam”, this.gameObject);

this will broadcast a message to the script.
Then use a public function within the other script, like this.

public void Inam ( GameObject receivedInam ){
    inam = receivedInam;
}

Now you sent the GO to the relevant script, and you can handle it accordingly.
( please note I prefer JS above C#, so minor corrections might be needed :wink: )