I have a battery pickup script that shows the text “Press E to pickup” when your looking at it. But for some reason it causes horrible lag and i don’t know why.
Here’s the code. C#
using UnityEngine;
using System.Collections;
public class battries : MonoBehaviour {
public float a;
public float b;
public float c;
public float d;
public bool showText;
public int Bat;
public GameObject Flight;
public int mainBat;
public bool safeRemove;
void Start()
{
showText = false;
}
void OnTriggerStay(Collider other)
{
showText = true;
if (!safeRemove)
{
if (Input.GetKeyUp(KeyCode.E))
{
mainBat = Flight.GetComponent<flashlight>().batLevel;
Bat = 100;
Flight.GetComponent<flashlight>().batLevel = Bat += mainBat;
safeRemove = true;
if (safeRemove)
{
Destroy(this.gameObject);
}
}
}
}
void OnTriggerExit(Collider other)
{
showText = false;
}
void OnGUI()
{
if (showText)
{
GUI.Box(new Rect(Screen.width / 2.55f, Screen.height / 124.98f, Screen.width / 3.78f, Screen.height / 16.1f), "Press 'E' to pickup");
}
}
}
Here’s an image of the profiler