Need to witch between multiple images by pressing 1,2,3,4,5...
Hi I'm making a script that switch between multiple images of weapons, depending on what weapon is used, so in this case I need to call different OnGUi functions but I cant D: This is the script I already making in c#, im a noob dont judje me pls ;D using UnityEngine; using System.Collections;
public class ChangeGun3 : MonoBehaviour
{
public Texture textureArma;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha3))
{
}
}
void OnGUI()
{
GUILayout.BeginArea(new Rect(700, 100, 500, 500));
GUI.DrawTexture(new Rect(10, 10, 100, 100), textureArma, ScaleMode.ScaleToFit, true);
GUILayout.EndArea();
}
}
Please help me! P.S. sorry for my english I'm Italian P.S Arma mean weapon.
Answer by ZefanS · Dec 07, 2015 at 07:56 AM
Since you want to display different images depending on which state you're in, you can use a variable to track that state and then adjust the GUI behaviour appropriately. This can be done entirely in one script. Something like this should be a decent starting point:
using UnityEngine;
using System.Collections;
public class ChangeWeaponImage : MonoBehaviour
{
public int weaponState;
// Use this for initialization
void Start()
{
weaponState = 1;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
weaponState = 1;
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
weaponState = 2;
}
//And so on...
}
void OnGui()
{
if (weaponState == 1)
{
//Draw your image for weapon 1
}
else if (weaponState == 2)
{
//Draw your image for weapon 2
}
//And so on...
}
}
It's pretty much just a brute-force way of doing it, so maybe it's not so elegant, but it should get the job done.
Hope this helps.