got lost in unity c#, came from XNA

Hi everybody, I’m sorry in advance if there is already some similar post, but I couldn’t find it. Also I’ve read the manual, but I’ve found no help there too.

So what is my problem. I’ve came from XNA and I can’t get the grip of the system, how things work in unity. Let’s make it clear on an example.

In XNA I’d have three classes, Circles, Circle and Ball. In my main game logic script, I’d make an instance of Circles, initialization of Circles’d create three instances of Circle, initialization of Circle’d make seven instances of Ball. OOP. I can’t figure out, how’d I do that in Unity. Can’t use new because of monobehaviour. I’m lost.

Hi, here’s a little tutorial but you’ll find much more everywhere online.

You create objects from the editor, for example you do GameObject > Create Other > Sphere then you drop on that newly created object your ball script (if you have/want one). You do the same with your circle using a cylinder for example and you can set the size to flatten it so it fits your actual circle need. You attach your circle script (if you have/want one)

Then for the circle, if it is some sort of logic to manage the previous object, you can create an empty game object and drop your Circles script on it.

Then if you want to re-use thoose object like in an instanciation process, you take each one and drop them from the hierarchy view (that is the current scene’s object hierachy) to you project view so it create “prefabs”, and in your script you can have public Transform variable in which you can drop your prefab in the editor. That variable allows you to Instanciate(thePrefab) from your script.

There is so much more goodness in Unity… I came from XNA as well (a long time ago), I never could get back to it :stuck_out_tongue:

the transition from xna to unity can be a little struggle but its worth it. all classes which should recive engine events (update, oncollision, etc) are monobehaviors which instances are attached to gameobject. you don’t create scripts with new (constructor) but you instantiate a gameobject with the script attached. you can have non-monobehavior classes for your own (abstract) stuff which has no representation in the game. but all classes which should be managed by the engine must be a monobehavior.
so you can either use the editor to place your configured gameobjects (prefabs) or you can have an empty gameobject with a script that spawns them on startup so they exist only at “runtime”.

I see, I could find the prefabs in the manual, but I didn’t realize how to build the objects hierarchy (like I described), I’ll surely take a look at the “prefabs - Transform var” thing and instead of trying to build it purely from the scripts (as I’d do in XNA), I’ll try to build it with the unity’s game objects… Thanks for the help!

have a look in the manual: Unity - Manual: Unity User Manual 2022.3 (LTS)
it should get you direction to get started.