//event rised where player enter into another body with collider and trigger void OnTriggerEnter2D(Collider2D other) { //get interacted item var interact = other.gameObject.GetComponent <InteractiveScript>(); if (interact != null) //check if this item exists { if (InteractItems.Count == 0) { curInteract = interact; } InteractItems.Add(interact); //add to interacted item collection GameObject.FindObjectOfType <LevelScript>().tooltipPanel.SetActive(true); //show tooltip about potential interaction GameObject.Find("TextTarget").GetComponent <Text>().text = curInteract.description; //show description about interaction } }
//same like above, but rised when exit void OnTriggerExit2D(Collider2D other) { var interact = other.gameObject.GetComponent <InteractiveScript>(); if (interact != null) { InteractItems.Remove(interact); //remove first item from collection if (InteractItems.Count == 0) { GameObject.FindObjectOfType <LevelScript>().tooltipPanel.SetActive(false); //if collectin is empty hide tooltip panel } else { curInteract = InteractItems[0]; GameObject.Find("TextTarget").GetComponent <Text>().text = curInteract.description; //or show next element description } } }
private void ChangeInteractTarget() { if (InteractItems.Count <= 1) { return; } int idx = InteractItems.IndexOf(curInteract); if (idx == InteractItems.Count - 1) { idx = 0; } else { idx++; } curInteract = InteractItems[idx]; GameObject.Find("TextTarget").GetComponent <Text>().text = curInteract.description; }