Search Unity

[SOLVED] How to Locate and Edit Raw Image

Discussion in 'Scripting' started by Lachlan_Aitken, Oct 23, 2019.

  1. Lachlan_Aitken

    Lachlan_Aitken

    Joined:
    Sep 16, 2019
    Posts:
    2
    This is my first time using Unity and Im slowly learning the ropes. I've been making a puzzle escape room game, and I wish to update a hotbar slot (A raw image) upon clicking on an object. It should remove the object, then set the first hotbar image to an image of the object. I believe I have incorrectly located which raw image I wish to edit, and was wondering how i find a path to that image. The Raw Image i wish to edit is under my Main Camera, currently named "Image"

    I Know when locating a Game Object, you can use the string of code:

    Object = GameObject.FindGameObjectWithTag("ObjectName");
    Is there a similar code format to find a Raw Image?
    .
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5. using UnityEngine.UI;
    6.  
    7. public class Interact : MonoBehaviour
    8. {
    9.     private RawImage Image1;
    10.     private Camera Camera;
    11.     private Texture Image1T;
    12.  
    13.     private void Awake()
    14.     {
    15.         Camera = Camera.main;
    16.         Image1 = Camera.GetComponent<RawImage>();
    17.     }
    18.  
    19.     private void OnMouseDown()
    20.     {
    21.         gameObject.SetActive(false);
    22.         Image1T = (Texture)AssetDatabase.LoadAssetAtPath("Assets/Textures/Ball.jpg", typeof(Texture));
    23.        Image1.texture = Image1T;
    24.     }
    25. }
    26.  
    27.  
    28.  
     
    Last edited: Oct 23, 2019
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Things like this are better to connect in inspector. Add [SerializeField] tag before RawImage declaration and the slot will appear in the script inspector. Drag the image object into that slot and it will always be there. Using things like Find and especially FindWithName hurts your game performance.
     
  3. Lachlan_Aitken

    Lachlan_Aitken

    Joined:
    Sep 16, 2019
    Posts:
    2
    Thank you so much, that was exactly what i needed!