I am trying to make a script so when you pick up an object, a checkmark shows up. The way I want to do this is by disabling the Image component on the Checkmark UI, and when IngredientCollected is true, have the checkmark show up by activation the Image component again. How could I do this? This is what I have so far. Also, I don't know if this will help you understand the code better or not but the public PickUp1 pickup1 is me accessing the variable IngredientCollected from that script and using it in this CollectCheck script. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UIElements; public class CollectCheck : MonoBehaviour { public PickUp1 pickup1; public Image checkmark; void Start() { checkmark = GetComponent<Image>(); checkmark.visible = false; } void Update() { if (pickup1.IngredientCollected == true) { checkmark.visible = true; } } }
Line 15 is not necessary if you are using line 10 to drag the Image in from the inspector. It's either-or: you either need to find it at runtime like you're doing, but this code won't find it if it is saved disabled, which is why traditionally a reference is dragged into line 10. If you drag a reference into line 10, and it is disabled, executing line 15 will set that reference back to null. Do only one or the other. If you absolutely must find it at runtime, make sure you use the variant of .GetComponent that will return disabled components. See docs.