- Home /
Moving from Java to Unity
Hello everyone,
I've been a Java developer for a few years now and I recently got involved in a game development project using Unity. I've messed around in Unity in the past, but never attempted a full game.
My question is how do you make a full game in Unity? I can't figure out where to store variables such as player stats and such. In Java I would just put all of them in the main class, but in Unity you can only add scripts to objects. Is there a way to make a script that runs without being attached to something and controls everything?
Any tips for moving from a programming language to a scripting engine are appreciated!
Thanks everyone for your help!
What's wrong with attaching it to an object? You can use playerPrefs to save the values of variables, and have an object that contains the script.
how about attaching it to the main Camera? or an empty game object that's always going to be in the scene?
UA works better if the title of our question is a question. Thinking of a good question-y title can also help give ideas where to Search.
This is a pretty common Q.
Try a Search on "unity game manager." Can also try "unity singleton" (obviously, from the java-trained, since only they use Design Patterns.) Generally it's any script on an empty with dontDestroyOnLoad. Others use a class with all statics.
Answer by whaleinthesea · Aug 14, 2015 at 08:56 PM
For beginners I recommend to take a look at http://www.it-ebooks.org/tag/unity You have many free ebooks about Unity. Here you can learn the basics(workspace,syntax etc.)
Answer by fares90 · Aug 14, 2015 at 05:08 PM
i am a java programmer like you (but not advanced,maybe intermediate but i am not sure ) after going through the first three projects in the learning site (roll ball,space shooter,nightmare) i think you should create an empty game object (call it main maybe that will help :))and attach a script to it that manage the game(what happen when the game start ,end ,background music, spawn enemies ,etc) and i think you cannot have an independent script . i hope that this will help and i recommend going through the projects in the learning site .(note:i am still a beginner in unity but that what i have learned from the projects , i am sorry if something was wrong:).
Answer by TotalAnnihilation · Aug 14, 2015 at 05:07 PM
Hey,
You could make an empty game object and attach your script to it, but I would say try to do it the "common" way.
Meaning, create a player, attach a script to it and inside that script declare his lifepoints and stuff. Then, make an enemy, attach a script to it containing his lifepoints etc. Then you could do a script for a weapon containing the damage and stuff. In Unity it is more component based.
Playerstats or something like the amount of players/enemys I would put, as suggested above, in an empty gameobject (call it GameManager or something) and then let all the other objects get their information from the GameManager object.
Hope that helped?
Answer by tanoshimi · Aug 14, 2015 at 05:13 PM
You can write all your game code in a single script attached to an empty Gameobject in the scene, and treat the Start() function in that as your main entry point. However, doing so would be fighting against the "Unity way" of doing things, rather than embracing it.
If you're a competent programmer, I recommend downloading one of the Unity sample projects from the asset store and dissecting the "correct" way of doing things....
Your answer
Follow this Question
Related Questions
Attacking Enemies at an Angle 0 Answers
Multiple Cars not working 1 Answer
Unity 5 - Adding a Pre-made Countdown Timer C# Script to the Game Scene as a Text UI Element 1 Answer
Firing projectiles on the XZ Plane 2 Answers
Distribute terrain in zones 3 Answers