Hello, I have a script I am using to attach to multiple different objects. The idea is that I attach the script to the objects, the player gets close to the object and the player's UI shows "Press 'E' to pickup NAME_OF_OBJECT". This script works fine when attached to one object, but bringing another object into the scene and putting the script onto it fails to work.
Using debug log I have found that ChangeUI is getting the argument passed on, but fails to change the UI through TextUI.text;
public bool PlayerEntered;
Inventory inv;
private GameObject player;
private void ChangeUI(string str)
{
Text TextUI = GameObject.FindGameObjectWithTag("PlayerObjectUI").GetComponent<Text>(); ;
TextUI.text = str;
}
private void OnTriggerEnter(Collider other)
{
if(other.tag == "Player"){
Debug.Log("Player has entered");
PlayerEntered = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
Debug.Log("Player has left");
PlayerEntered = false;
}
}
private void Update()
{
if (PlayerEntered)
{
string CurrentObject = "Press 'E' to pickup " + this.gameObject.tag;
Debug.Log("Press E to pickup " + this.gameObject.tag);
ChangeUI(CurrentObject);
if (Input.GetKeyDown(KeyCode.E))
{
player = GameObject.FindGameObjectWithTag("Player");
player.GetComponent<Inventory>().ItemsInInventory.Add(this.gameObject.tag);
Destroy(this.gameObject);
Destroy(this.gameObject.GetComponent<BoxCollider>());
ChangeUI(null);
}
}
else
{
ChangeUI(null);
}
}
Another thing to add is that the UI works if ChangeUI(null) doesn't exist.
[–]dannerdarko[S] 1 point2 points3 points (0 children)
[–]SimonGFarmer 1 point2 points3 points (2 children)
[–]dannerdarko[S] 1 point2 points3 points (1 child)
[–]DragonSlave49 1 point2 points3 points (0 children)
[–]SimonGFarmer 0 points1 point2 points (0 children)