Does anyone know what to put where the underscores are? I want to change an image but don’t know what to put…
foreach (RawImage image in _____)
Does anyone know what to put where the underscores are? I want to change an image but don’t know what to put…
foreach (RawImage image in _____)
something like, if you want to assign them in inspector
public RawImage[] myarray;
...
foreach (RawImage image in myarray)
or, you might want to get them in child gameobjects with,
RawImage[] myarray = GetComponentsInChildren<RawImage>()
Actually, that would probably work for a different script, but not this one… Do you know if I could disable and enable the RawImages with the code I already have? I am trying to make a UI that changes gun images when I switch weapons.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class WeaponSwitcher : MonoBehaviour
{
public Animator animator;
public RawImage gun1UI;
public RawImage gun2UI;
public int selectedWeapon = 0;
public bool isSwitching = false;
public float switchTime = 2f;
void OnEnable()
{
isSwitching = false;
animator.SetBool("isSwitch", false);
}
// Start is called before the first frame update
void Start()
{
SelectWeapon();
}
// Update is called once per frame
void Update()
{
int previousSelectedWeapon = selectedWeapon;
if (Input.GetAxis("Mouse ScrollWheel") > 0f)
{
if (selectedWeapon >= transform.childCount - 1)
selectedWeapon = 0;
else
selectedWeapon++;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0f)
{
if (selectedWeapon <= 0)
selectedWeapon = transform.childCount - 1;
else
selectedWeapon--;
}
if (previousSelectedWeapon != selectedWeapon)
{
SelectWeapon();
StartCoroutine(SelectWeapon());
}
}
IEnumerator SelectWeapon()
{
isSwitching = true;
animator.SetBool("isSwitch", true);
yield return new WaitForSeconds(switchTime);
int i = 0;
foreach (Transform weapon in transform)
{
if (i == selectedWeapon)
weapon.gameObject.SetActive(true);
else
weapon.gameObject.SetActive(false);
i++;
animator.SetBool("isSwitch", false);
isSwitching = false;
}
}
}