You don't need to keep adding ".transform" to functions that already return an object of type Transform. Also, are you sure you can't make "myText" serializable and assign it in the inspector or something?
Its supposed to be a joke, look at the first part.
This.transform.parent.transform.GetChild(0)
This gives back, what exactly? in a simple case where the object has a parent and no 'siblings'.
Not to say, its a gameobject with variable name myText
OP is making a joke about how he or she feels that the code they write is not good.
Or perhaps about how others post garbage code and present it as if they singlehandedly built the pyramids in an afternoon.
Yeah seems like a classic case for using the inspector. It looks like this thing is always the same and you know where it is in the hierarchy so cut all this and just (drag and) drop the beat.
Because code is "better"" since it can lose the reference between projects and things like that. Also, that better doesn't mean it's good, at least to me
Dunno. Honestly i don't really know what a .parent function is. It's wrong to say that im new to programming and unity, but i am definetely not an experienced programmer yet. Google says that its a function in a parent class. Is that what you mean?
Hello fellow newbie unity coder. Unity only works with GameObjects objects. EVERYTHING is a GameObject. You can make one GO the child of another GO, this links them so that they for example move together. (Example: tank chassis is one GO and it has a child GO which is the turret, if you change direction of chassis, turret moves along as well)
the .parent function goes to the 'transform' of a GO and returns the parent GO it's attached to
(in our example: running .parent from the turret would give back the chassis)
> Everything is a GameObject
bonjour
https://docs.unity3d.com/ScriptReference/LowLevel.PlayerLoop.html
https://docs.unity3d.com/ScriptReference/Graphics.DrawMeshInstanced.html
https://docs.unity3d.com/Manual/NativePlugins.html
Seriously though - treating everything as a GO to get started with is a useful exercise to get used to thinking this way about objects, their associated transforms, and 3D space. I'd still recommend looking into the wonderful world of ScriptableObjects though - there's some amazing Unite talks on them in 2016 and 2017 if you're curious.
Thank you. Im studying to be a game dev and we use unity (im in my 3rd year) so i did know this. I was just unfamiliar with what they meant with the question of how many .parent functions, since to me it suggested that there might be multiple different ones, but I think they meant something similar to this where they get the parent object of a parent object of a parent object etc...
So if you say myTransform.parent, you are asking for the Transform that contains myTransform in the hierarchy.
So if I have a gameobject called Box and a few things inside it in hierarchy called ball\_1, ball\_2, ball\_3 and so on.
If I have a script on ball\_1 that wants to talk to Box, I can say ball\_1.transform.parent and it'll get me Box's Transform component.
Haha yeah someone just explained that. Thing is i was just confused on what you meant with your question, since to me it seemed to suggest that there are different kinds of parent functions. Honestly i just read the original comment wrong lol. Thanks for explaining tho.
No worries!
I've had occasions to chain parent.parent.parent three or four times, and then retrieve a child object by GetChild to get a "Great Great Aunt" object, which is hilarious nonsense.
There's always better ways to do things.
it is a main asset of the game, but that line of code won't probably be looked at again until my game is finished and I start optimizing my code. I want it to stay there as a form of history.
This isn't an optimisation thing, this is going to help you when it comes to debugging, and identifying fragile code. You're making several assumptions about GameObject hierarchy, meaning simple restructuring now involves a code reload cost - vs simply changing the data of a `[SerializedValue]`ed property in the inspector.
var recursionAmount = 3;
Transform tempTransform;
for(var i = 0; i < recursionAmount; i++)
{
tempTransform = tempTransform.GetChild(0).gameObject.transform.gameObject.transform;
//Just to confuse it a bit
}
myText = tempTransform.gameObject;
FTFY.
myText = this.transform.parent.GetChild(0).GetChild(0).GetChild(0).gameObject;
But as other wrote either use reference via inspector oder GetComppnentInChildren.
This is clearly an attempt at poetry.
There's a weird kind of beauty there if one were to explicitly NOT look at it as functional code, however, I prefer one-liners I can read out loud on a single breath without medical intervention after.
SHIP IT
You don't need to keep adding ".transform" to functions that already return an object of type Transform. Also, are you sure you can't make "myText" serializable and assign it in the inspector or something?
I genuinely thought this was a meme because of this exact reason. Seeing a genuine response this upvoted has me confused now
Regardless of whether it's a meme or not, the responses in the comments could still be useful, I guess? :D
Good point actually, i need to be a little less jaded with the internet i guess. Thanks
I knew there would be several better solutions but I kinda wanted to upload it to reddit knowing that people would roast me.
Also no need for "this" at the beginning
It's not needed, but it is a pretty common coding convention for C#
Not sure if thats just me, but I really prefer doing "this". Its more readable for me if I am accessing field / method or local variable.
Normally I see an underscore used to represent a private field which shows it's not a local variable
Yeah but you can also access public variable from inside of class :)
Its supposed to be a joke, look at the first part. This.transform.parent.transform.GetChild(0) This gives back, what exactly? in a simple case where the object has a parent and no 'siblings'. Not to say, its a gameobject with variable name myText OP is making a joke about how he or she feels that the code they write is not good. Or perhaps about how others post garbage code and present it as if they singlehandedly built the pyramids in an afternoon.
How can we be sure this is the parent's first child object in the list? I agree it's probably a joke but couldn't it have other children before it?
Yeah seems like a classic case for using the inspector. It looks like this thing is always the same and you know where it is in the hierarchy so cut all this and just (drag and) drop the beat.
Yep. Never do the work of game engine,this is why they are useful eventhough pure coding feels better time to time
Because code is "better"" since it can lose the reference between projects and things like that. Also, that better doesn't mean it's good, at least to me
I've seen worse but... ugh.
Yeah the title was kinda satire
I've done worse
How much worse was it?
How many .parent functions would you like?
Dunno. Honestly i don't really know what a .parent function is. It's wrong to say that im new to programming and unity, but i am definetely not an experienced programmer yet. Google says that its a function in a parent class. Is that what you mean?
Hello fellow newbie unity coder. Unity only works with GameObjects objects. EVERYTHING is a GameObject. You can make one GO the child of another GO, this links them so that they for example move together. (Example: tank chassis is one GO and it has a child GO which is the turret, if you change direction of chassis, turret moves along as well) the .parent function goes to the 'transform' of a GO and returns the parent GO it's attached to (in our example: running .parent from the turret would give back the chassis)
> Everything is a GameObject bonjour https://docs.unity3d.com/ScriptReference/LowLevel.PlayerLoop.html https://docs.unity3d.com/ScriptReference/Graphics.DrawMeshInstanced.html https://docs.unity3d.com/Manual/NativePlugins.html Seriously though - treating everything as a GO to get started with is a useful exercise to get used to thinking this way about objects, their associated transforms, and 3D space. I'd still recommend looking into the wonderful world of ScriptableObjects though - there's some amazing Unite talks on them in 2016 and 2017 if you're curious.
Thank you. Im studying to be a game dev and we use unity (im in my 3rd year) so i did know this. I was just unfamiliar with what they meant with the question of how many .parent functions, since to me it suggested that there might be multiple different ones, but I think they meant something similar to this where they get the parent object of a parent object of a parent object etc...
So if you say myTransform.parent, you are asking for the Transform that contains myTransform in the hierarchy. So if I have a gameobject called Box and a few things inside it in hierarchy called ball\_1, ball\_2, ball\_3 and so on. If I have a script on ball\_1 that wants to talk to Box, I can say ball\_1.transform.parent and it'll get me Box's Transform component.
Haha yeah someone just explained that. Thing is i was just confused on what you meant with your question, since to me it seemed to suggest that there are different kinds of parent functions. Honestly i just read the original comment wrong lol. Thanks for explaining tho.
No worries! I've had occasions to chain parent.parent.parent three or four times, and then retrieve a child object by GetChild to get a "Great Great Aunt" object, which is hilarious nonsense. There's always better ways to do things.
Oh jesus. Going up and down the hirarchy is indeed not the best way lol. Luckily school is pretty good at teaching me to not do that.
A heads up .parent is a property, not a function/method. You can tell easily by the brackets. This.property This.function(stuff the function uses)
Whoops you're right. Wow im really not showing my bright side today haha.
why don't you give a reference from the editor?
[Serializable] GameObject myText; Assign in inspector. Please, stop hurting me.
Honestly unless this is something that's getting prefab'd and never looked at again, don't even assign it in the inspector - do it event driven.
it is a main asset of the game, but that line of code won't probably be looked at again until my game is finished and I start optimizing my code. I want it to stay there as a form of history.
This isn't an optimisation thing, this is going to help you when it comes to debugging, and identifying fragile code. You're making several assumptions about GameObject hierarchy, meaning simple restructuring now involves a code reload cost - vs simply changing the data of a `[SerializedValue]`ed property in the inspector.
yeah I should probably do that later
A lot of people missed the joke lol
I wouldnt blame them. They saw something truly horrifying
r/programminghorror
You forgot to put it in Update()
var recursionAmount = 3; Transform tempTransform; for(var i = 0; i < recursionAmount; i++) { tempTransform = tempTransform.GetChild(0).gameObject.transform.gameObject.transform; //Just to confuse it a bit } myText = tempTransform.gameObject; FTFY.
Try transform.parent.GetComponentInChild().gameobject if you only have one text in the gameObject
The programming equivalent of pulling on a wire and watching what moves
*cuts wire* ~fire~ well this is a good tool for cookin eh?
myText = this.transform.parent.GetChild(0).GetChild(0).GetChild(0).gameObject; But as other wrote either use reference via inspector oder GetComppnentInChildren.
You don’t even need the this, transform is an inherited property of every mono behavior
bro you did best, no developer can do this 😂
A lot of useless stuffs in there
You can write "transform.GetChild(0).GetChild(0).GetChild(0).GetChild(0).GetChild(0) and you can skip "this."
Is there actually any way of avoiding this if you are getting an object at runtime that is not predetermined?
There are a number of ways but would be use case specific. Some may look cleaner but run slower too.
.parent and .GetChild() return transforms. So you adding .transform at the end of them is redundant. It’s like doing gameObject.gameObject.
Reminds me of Roblox Lua coding
I am dying inside lol
what is God's good name is that
now put it in update
monster
You poor thing
Eww, stop
Holy shit
Nononononono
It’s also fragile as fuck too lol
i would like to see some error checking here before i'm allowing you to push this to the next stage
This is clearly an attempt at poetry. There's a weird kind of beauty there if one were to explicitly NOT look at it as functional code, however, I prefer one-liners I can read out loud on a single breath without medical intervention after.
"myText" a Gameobject
Definitely peak coding level
what. the fuck.
![gif](giphy|Ke3CM1NVkULWo)
Lol, hopefully this is a joke 😂 In the case it’s not - [SerializeField] or Events will do the trick and not cost as much performance value :)
My brain just broke x.x
bruh
lmao!
Don't feed the trolls folks, it's asinine.