#Working with lists
1 messages · Page 1 of 1 (latest)
I don't get it...
Why wouldn't this just look like:
public void PerformLogic() {
PositionElements();
health.fontSize = reference;
}
public void PositionElements()
{
for(int i = 0; i < elements.Count; i++) {
var rt = (RectTransform)elements[i].transform;
rt.anchoredPosition = new Vector2(reference * (i * 3), reference);
rt.sizeDelta = whatever;
}
}```
I am not really sure our code is doing the same thing but I might be just bad at c#, here is the code I have as of right now:
https://gdl.space/oxeluzudel.cs
but from what I've found online there is no workaround of just using list to assign values to variables, I'll have to do it manually so I can avoid any problems later
There is no logical connection between how you position these elements, why would you put them in a list?
It's all just magic numbers
look at GetHUDElements, instead of just doing that for loop I'd have to assign value to every single one of them
I was trying to avoid that in the first place but it turns out it's not making anything easier after all
I find the whole thing very odd because it seems like you're referencing things you don't actually care about. Why reference the GameObject of the health icon if you're not directly using it?
If you did care about the child RectTransforms of so many objects, why not just say
[SerializeField] private RectTransform healthIcon, health, armourIcon, armour, timerIcon, timer, jetpackIcon, hookIcon, flashlightIcon;
Then drag and drop those child transforms into the inspector, and do the positioning logic on them.
If you care about the text on the armour, have
[SerializeField] private TextMeshProUGUI armourText, timer;
if you read anything I sent before you'd know that I need the whole game object because rect transform is not the only thing that I want and I am not using [SF] in this case because I like to get stuff like this trough code and make sure it works everywhere, [sf] can lead to many problems if you are not careful, don't drag something where it should be or move something so I avoid that generally
also I do need those things outside of the list which can also be seen in my code but it's commented because of an error and I mentioned it before
If you care about multiple objects in a similar way, make a script that you reference on the object that groups that logic together.
Say if you cared about the root object, the child, and some text:
public class TextGroup : MonoBehaviour {
public RectTransform Child;
public TextMeshProUGUI Text;
}```
Then you reference
`[SerializeField] private TextGroup armourText, timer;`
to easily get `armorText.gameObject`, `armorText.Child`, or `armorText.Text`
If you want to automate the discovery of those objects, assign them in Awake of the TextGroup class
I asked a simple question, was it possible to assign values to vars in list or no. I already know how lists work, how to get different types of components, how to use sf and all that, my question was very direct and could've been answered in 1 word yes/no
Great, happy to help then
I appreciate you trying to help and explain all of this but I really don't need it and I am in a hurry to finish this code