[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Hello /g/, I'm fairly new to Unity and trying to build a

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 22
Thread images: 1

File: ogimg.jpg (17KB, 1200x630px) Image search: [Google]
ogimg.jpg
17KB, 1200x630px
Hello /g/,
I'm fairly new to Unity and trying to build a simple game in C#. I understand that every script you create needs to be attached to a GameObject. Now say I have 3 GOs. What is the best practice to interact with them? I usually do the following to separate tasks - it works fine, but seems pretty stupid as it kind of defies object-oriented-programming.

>GameObjects A, B and C

[A] (takes, e.g., UI input values)
GameObject B, C;

void Start() {
B = GameObject.Find("B"); //this style is bugging me
C = GameObject.Find("C"); //this style is bugging me
}

private void doShit() {
//do shit in A and then call some functions in B or C, maybe with some parameters from A
B.GetComponent(Bscript).somefunction();
C.GetComponent(Cscript).somefunction(someMember);
}


[B]
GameObject A, C;

void Start() {
A = GameObject.Find("A"); //this style is bugging me
C = GameObject.Find("C"); //this style is bugging me
}

private void doMoreShit() {
//nice data mangling here. then we need a parameter from A to continue.
//so we could either call B.doMoreShit() from A with the parameter in the first place or get it here separately
niceFunction(A.someUIinputParameter);
C.GetComponent(Cscript).somefunction(someMember);
}


OR: We declare public variables and fill them with the appropriate GOs. It seems Unity gives several possibilities to achieve the same thing. So what's best practice? I kind of like to keep it OO without too much click'n'drag in the inspector.
Maybe use an Event System...?
>>
post on unity forums faggot
>>
>>60539360
use zenject
https://www.youtube.com/watch?v=8hru629dkRY
>>
>>60539360
Use variables and drag
>>
Full time gamedev here.

Just declare public variables and drag gameobject in inspector, in your case. It's simple and just werks. Avoid using gameobject.find() or findobjectsoftype() as much as possible. They're slow as fuck
>>
>>60539489
or that >>60539532
also let this chubby fella teach you about scriptable objects while you're at it
https://youtu.be/6vmRwLYWNRo?t=573
>>
>>60539557
hmm i neither like the find-functions nor the clicking and dragging objects in the inspector. once you have a slightly more complex UI or game you can easily lose the oversight, especially if the class gets reset in the inspector for some reason and you have to re-drag everything.
i think i'm going to look into an event-based approach... not 100% sure though how to go about it
>>
>>60539489
Don't add useless bloat to your project Jesus fucking Christ you stupid nigger pajeets

>>60539595
Event based is slow as fuck. Avoid unity events. Inspector was is best from my years of dealing with unity bullshit even in huge projects. Inspector doesn't randomly reset if you make an object a prefab like you should. If something does reset, hit assets -> reimport all
>>
>>60539620
>Don't add useless bloat to your project Jesus fucking Christ you stupid nigger pajeets
by that logic he shouldn't use unity in the first place
>>
>>60539620
hmmmhmhmm ok. it doesn't quite convince me yet. i like to use scripts as much as possible because dragging objects to the variables in the inspector doesn't seem like the best idea in the long run. it's easy for simple projects, i'll give you that
>>
>>60539652
>by that logic he shouldn't use unity in the first place
because it's so much easier to just write your own graphics engine thats portable to android and iOS.
>>
>>60539666
We're doing it on our project that's two years in Dev right now man. It's not anymore complicated as the project grows unless your design is trash. If it's too complicated for you, consider suicide and give up your gamedev dreams
>>
>>60539666
I agree with your sentiment but there isn't a better alternative
>>
>>60539695
ok i'll become an astronaut now
>>
>>60539360
>>>/vg/177561035

Try going here, muh dude.
>>
>>60539360
You should really watch the Roll a Ball tutorial for unity. He explains how to interact with other game objects
>>
>>60540193
ooh nice. sorry man, didn't even know that board existed. thank you!
>>
>>60540215
>Roll a Ball
nice! didn't know about that either. great info
>>
>>60539666
Unity is simply designed that way. It really is best practice to use drag n drop even on large objects. Look up unity MVCC for an interesting approach to handle hierarchy
>>
>>60539360
>unity
underage
>>
>>60539557
Yes this


>>60539360

Making a public variable and assigning these variables via the editor is a really great way of doing things. You could also declare the variables as private and give them the [SerializeField] attribute. This will allow you to assign the private variables in the editor, and increase your class encapsulation.

I'd try to avoid using any find methods as much as possible. When you call one of these find methods, you are adding more responsibility and knowledge to your object, which makes them more fragile and easy to break.

When you assign via the editor, you are performing dependency injection. You 'inject' the dependency into the object with the editor. The object doesn't have to care about how it gets the dependency. At this point the object only knows that it has a reference to some other object. It can then worry about doing whatever it needs to do on that object.

For more event based stuff where you broadcast events such as "Game Over" or "Paused", you should look into C# Actions and read up about the Observer pattern.
>>
>>60542531
>>60542531
great info bro. thanks for the post.
i got some other info here and added some details on what kind of manager i actually need:
>>>/sci/8930086
Thread posts: 22
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.