Hi! In my battle with C# and Unity now I want to check if some objects from a List are visible or not in the UI canvas to enable other features.
I have this:
public List<TimelineFireworkPoint> Points => addedPoints;
And I want to check if they are visible or not with “if (GetComponent().isVisible)” for each one of them and run some code. I am still new to understanding how unity works but I always get errors like “…does not contain a definition for…”
Thank you in advance.
EDIT: A typo: TimelinePun is TimelineFireworkPoint
Perhaps show the code you have. Otherwise, looping through a list and checking values isn’t difficult, it’s just knowing what you need to check against.
Since you mention UI canvas, is this a scrolling list and you want to see what is currently in the visible portion of the list? (Using a scrollview that is?) Thus you want to do something as a player scrolls?
I was looking is something like this but I’m reaching the same problem, now with isVisible: "‘TimelineFireworkPoint’ does not contain a definition for ‘isVisible’.
Yes, It’s a scroll view rect horizontally, that has the Points on it. This is for Show/Hide the objects this points contain, not this points:
foreach(var item in addedPoints) {
item.FireworkObject.Show(); //SHOW OR Hide() OBJECTS
}
The “public List Points => addedPoints;” are the objects I want to check if they are visible or not on the screen when I scroll and then execute show/hide Objects, like this but I have the error:
foreach(var item in addedPoints) {
if(item.isVisible) {
item.FireworkObject.Show();
}
}
This is the code with objects inside TimelineFireworkPoint script (only the objects that are necessary for this problem):
public class TimelineFireworkPoint : MonoBehaviour
{
public float CurrentTime { get; private set; }
[SerializeField] private Image image;
[SerializeField] private TextMeshProUGUI numberLabel;
private Canvas canvas;
private TimelineController timelineCtrl;
public FireworkObject FireworkObject { get; private set; }
And this code with objects inside FireworkObject script (only the objects that are necessary for this problem) :
ok I’m not versed with that particular combination of components you’re using, but does your object have isVisible property or not? if it’s a scrollrect or whathaveyou (I’m a bit rusty with the latest UI atm) how would you ordinarily query whether something is visible or not?
if your answer is “x then y” then you already know everything there is to know.
if your answer is “I don’t know” then your original question should have been that.
Ok, so, I am still not sure what you are attempting to do. But, if you want to know if something is visible, you have to use a component that has that type of value or has a method that already does that (like some of the render components do). Or, you have to manually determine if it’s visible. There are many ways to do this. Sometimes it’s as easy as just checking if it’s within your screens view. If you know the max and min x position and you get the position of the object and it’s between there, then you know it should be visible. There are certainly several ways it can be accomplished.
But, looking at your little image with the 3 white dots, I thought I had an idea of what you wanted, but then I was like, maybe I don’t actually.
So, if that is a scrollview and it’s going horizontal, are you just trying to check when a white dot becomes visible so you can display something else on screen? Or is the dot itself what is being turned on or off?