So I am making a fps game and I am trying to use GUI features to draw a sniper rifle icon when a sniper is selected, and a rangefinder icon when the rangefinder is selected. I wrote a script and everything, but I get error saying “You can only call GUI features from OnGUI”. Which I already am doing. Also, the sniper icon is not changing into the rangefinders when the rangefinders are selected. PLEASE HELP!! THX.
Here is my code and the in game errors:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class DrawWeaponScript : MonoBehaviour
{
public Texture sniperIcon;
public bool SniperOut;
public bool RangeFinderOut;
public Texture rangefinderIcon;
// Use this for initialization
void Start () {
SniperOut = true;
RangeFinderOut = false;
rangefinderIcon = Resources.Load("RangeFinderIcon") as Texture;
sniperIcon = Resources.Load("Sniper Icon") as Texture;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("2"))
{
SniperOut = false;
RangeFinderOut = true;
}
if (Input.GetKeyDown("1"))
{
SniperOut = true;
RangeFinderOut = false;
}
OnGUI();
}
void OnGUI()
{
//Draw the Current Inventory Selection Image
if (SniperOut = true)
{
GUI.DrawTexture(new Rect(20, 20, 100, 100), sniperIcon);
GUI.color = Color.black;
}
else if (RangeFinderOut = true)
{
GUI.DrawTexture(new Rect(20, 20, 100, 100), rangefinderIcon);
GUI.color = Color.white;
}
else
{
//Do nothing
}
}
}
In Game Screenshot: