Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Changing Images with foreach (RawImage image in _____)

Discussion in 'Editor & General Support' started by Mr_AgentFox, Jul 31, 2019.

  1. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59
    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 _____)
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
  3. Mr_AgentFox

    Mr_AgentFox

    Joined:
    Jun 23, 2019
    Posts:
    59

    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. public class WeaponSwitcher : MonoBehaviour
    5. {
    6.  
    7.     public Animator animator;
    8.     public RawImage gun1UI;
    9.     public RawImage gun2UI;
    10.  
    11.     public int selectedWeapon = 0;
    12.     public bool isSwitching = false;
    13.     public float switchTime = 2f;
    14.  
    15.  
    16.     void OnEnable()
    17.     {
    18.         isSwitching = false;
    19.         animator.SetBool("isSwitch", false);
    20.     }
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         SelectWeapon();
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.  
    32.         int previousSelectedWeapon = selectedWeapon;
    33.  
    34.         if (Input.GetAxis("Mouse ScrollWheel") > 0f)
    35.         {
    36.             if (selectedWeapon >= transform.childCount - 1)
    37.                 selectedWeapon = 0;
    38.             else
    39.                 selectedWeapon++;
    40.         }
    41.  
    42.         if (Input.GetAxis("Mouse ScrollWheel") < 0f)
    43.         {
    44.             if (selectedWeapon <= 0)
    45.                 selectedWeapon = transform.childCount - 1;
    46.             else
    47.                 selectedWeapon--;
    48.         }
    49.  
    50.         if (previousSelectedWeapon != selectedWeapon)
    51.         {
    52.             SelectWeapon();
    53.             StartCoroutine(SelectWeapon());
    54.         }
    55.     }
    56.  
    57.     IEnumerator SelectWeapon()
    58.     {
    59.         isSwitching = true;
    60.         animator.SetBool("isSwitch", true);
    61.         yield return new WaitForSeconds(switchTime);
    62.         int i = 0;
    63.  
    64.         foreach (Transform weapon in transform)
    65.         {
    66.             if (i == selectedWeapon)
    67.                 weapon.gameObject.SetActive(true);
    68.             else
    69.                 weapon.gameObject.SetActive(false);
    70.             i++;
    71.  
    72.             animator.SetBool("isSwitch", false);
    73.             isSwitching = false;
    74.         }
    75.     }
    76. }
    77.