Need help coding please,I need help with code
I'm trying to make a script that when you walk up to an object (in this case a book) it tells me on-screen press a key to read and then it enables a game object. This is what I have could someone tell me what I'm doing wrong?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ReadTest : MonoBehaviour { public GameObject book; bool enter = false;
// Start is called before the first frame update
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
book.SetActive(true);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
enter = true;
}
}
void OnGUI()
{
if (enter)
{
GUI.Label(new Rect(Screen.width / 2 - 75, Screen.height - 800, 850, 500), "Press 'E' to read the book");
}
}
}
And, what is your problem?
As far as I can tell, you don't check in the Update
whether you have collided with the player, so you can enable the book at any time (except if the script is attached to the disabled book). And you don't check whether you leave the trigger zone, which should prevent you from enabling the book.
Your answer
Follow this Question
Related Questions
When I duplicate my UI, navigation stops working 2 Answers
When Near a door, Press a button to go to the other side. 1 Answer
VR - Best method for keeping track of both controllers are interacting with an object. 0 Answers
Collider.OnTriggerStay is dropping my frame rate from 2000fps to 0.5fps 1 Answer
Triggers won't recognize tags other than the actual player 0 Answers