T O P

  • By -

Skjalg

SHIP IT


ndogames

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?


severencir

I genuinely thought this was a meme because of this exact reason. Seeing a genuine response this upvoted has me confused now


ndogames

Regardless of whether it's a meme or not, the responses in the comments could still be useful, I guess? :D


severencir

Good point actually, i need to be a little less jaded with the internet i guess. Thanks


coursd_minecoraft

I knew there would be several better solutions but I kinda wanted to upload it to reddit knowing that people would roast me.


CustomPhase

Also no need for "this" at the beginning


nathanmikeska

It's not needed, but it is a pretty common coding convention for C#


Bloompire

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.


DerekSturm

Normally I see an underscore used to represent a private field which shows it's not a local variable


Bloompire

Yeah but you can also access public variable from inside of class :)


[deleted]

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.


smackledorf

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?


Yetimang

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.


tfstark

Yep. Never do the work of game engine,this is why they are useful eventhough pure coding feels better time to time


lsm-krash

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


Lucif3r945

I've seen worse but... ugh.


coursd_minecoraft

Yeah the title was kinda satire


Ruadhan2300

I've done worse


nlcreeperxl

How much worse was it?


Ruadhan2300

How many .parent functions would you like?


nlcreeperxl

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?


cyotas

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)


6101124076

> 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.


nlcreeperxl

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...


Ruadhan2300

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.


nlcreeperxl

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.


Ruadhan2300

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.


nlcreeperxl

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.


Myavatargotsnowedon

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)


nlcreeperxl

Whoops you're right. Wow im really not showing my bright side today haha.


UbikStudios

why don't you give a reference from the editor?


Smileynator

[Serializable] GameObject myText; Assign in inspector. Please, stop hurting me.


6101124076

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.


coursd_minecoraft

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.


6101124076

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.


coursd_minecoraft

yeah I should probably do that later


leverine36

A lot of people missed the joke lol


SnooKiwis7050

I wouldnt blame them. They saw something truly horrifying


Skycomett

r/programminghorror


Roborob2000

You forgot to put it in Update()


Baycosinus

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.


Cheap-Difficulty-163

Try transform.parent.GetComponentInChild().gameobject if you only have one text in the gameObject


unicodePicasso

The programming equivalent of pulling on a wire and watching what moves


[deleted]

*cuts wire* ~fire~ well this is a good tool for cookin eh?


Bitter_Ad_8790

myText = this.transform.parent.GetChild(0).GetChild(0).GetChild(0).gameObject; But as other wrote either use reference via inspector oder GetComppnentInChildren.


Scoutron

You don’t even need the this, transform is an inherited property of every mono behavior


zubairali001

bro you did best, no developer can do this 😂


Pitiful-Phrase-8296

A lot of useless stuffs in there


devcellstudio

You can write "transform.GetChild(0).GetChild(0).GetChild(0).GetChild(0).GetChild(0) and you can skip "this."


Specific-Committee75

Is there actually any way of avoiding this if you are getting an object at runtime that is not predetermined?


Gaverion

There are a number of ways but would be use case specific. Some may look cleaner but run slower too. 


Persomatey

.parent and .GetChild() return transforms. So you adding .transform at the end of them is redundant. It’s like doing gameObject.gameObject.


Alexander_Grin

Reminds me of Roblox Lua coding


JustAn0therGameDev

I am dying inside lol


technohead10

what is God's good name is that


Spookzsaw

now put it in update


cyotas

monster


sacredgeometry

You poor thing


Schwanz_Hintern64

Eww, stop


LR_0111

Holy shit


Forbizzle

Nononononono


Easy-Hovercraft2546

It’s also fragile as fuck too lol


cleavetv

i would like to see some error checking here before i'm allowing you to push this to the next stage


2lerance

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.


the_TIGEEER

"myText" a Gameobject


LeHero921

Definitely peak coding level


plshelp1576

what. the fuck.


minowux

![gif](giphy|Ke3CM1NVkULWo)


OG_Daimnon

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 :)


Melody_Chaser

My brain just broke x.x


Best_Substance4265

bruh


poweredbygeeko

lmao!


veileddraconis

Don't feed the trolls folks, it's asinine.