Unity adding prefabs on runtime - c#

I'm very new to Unity and have a question regarding loading of game objects at runtime. I have only 4 scenes (4 different 'surroundings'). Each of them displays a 3D object - the one that the user selects from the menu. There will be around 60 3D objects (but only one at a time is displayed). How to deal with loading them? I don't want to create 60 × 4 scenes and add them as a prefab to a scene individually, but keep only 4 scenes and programmatically add 3D objects to them to the scene. What is the best practice memory & performance wise? Can they be reused? How to approach this problem if objects are very big? Are there any code samples?

You can have a script on each scene, referencing 60 prefabs (there's an easy trick to it - if have a public List and want to drag multiple objects from the project window, just lock your inspector using a padlock, you can then select multiple prefabs in the project window and drag them all in one move, releasing on the NAME of the list, instead of expanded empty field).
Prefabs are shared within a project so they won't get duplicated between scenes.
You could also have them all on the scene to start with, deactivated, and just activate one selected prefab, this will be slightly easier, but will use more memory, in terms of build size this should make no difference. If the objects are big you are probably better of instantiating and destroying them as you go

I’m far from being an expert but the way I have done it is with instantiate.
Practically you generate from a prefab a gameObject and later (if it isn’t needed anymore) you Destroy(gameObject);
Instantiate(prefab, new Vector3(x, y, z), Quaternion.identity);

Related

Unity not rendering animation in playmode

My door animation plays all fine in Editor, but in playmode, it seems not to render, but the collisions seem to work fine. Here's what happens.
The Animator is set up like this
The Animation the game should show consists of just moving two children objects, for more info. What did I do wrong, and how do I fix it?
It pretty much seems like your objects are marked as static and therefore the meshes get combined into a single static scene mesh.
Many systems in Unity can precompute information about static GameObjects in the Editor. Because the GameObjects do not move, the results of these calculations are still valid at runtime. This means that Unity can save on runtime calculations, and potentially improve performance.
As the name static already suggests: These objects are static and can not be moved by the Animator.

Simulating a second, hidden physics scene into the future on a single frame to predict the trajectories of planets?

I want to simulate the trajectories of planets in a seperate scene to find out where they will be in the future. I drew a quick diagram to demonstrate.
Is there a way to simulate 2 scenes separately, hiding one but showing the other? I tried this which says they don't interact with each other, but when I tried it they still collided.
Yes, there is a way, if you put all "hidden" objects, like planets on a reparate layer in unity
and disable collisions between that and any other layers in the
edit/project setting/physics
This way those objects won't have any effect on the rest of your scene.
To visually hide the objects simply disable the rendering of that layer in your scene camera. And that's it, if hope this was helpful.

How to communicate between 2 scenes in Unity?

I would like to make a color system for my player. In my game scene the player can pick up the coins and the amount of coins will be save with playerprefs , but I don' know how can I use the amount of coins in my menu scene.
And I need some help to a player color selecter too. When the player select a color than in my game scene must instiate the player with the color.
Soo I think , i need to know how to communicate between 2 scenes.
Can somebody help me with some tutorial?
There are multiple ways to achieve this, which I feel comes down to a matter of taste: save data to file, use DoNotDestroyOnLoad...
But from what I understand, the recommended way now is to create a "manager scene" which will stay alive throughout the lifetime of your app and pass data to and from your other scenes as they are opened and closed, instead of using DontDestroyOnLoad:
It is recommended to avoid using DontDestroyOnLoad to persist manager GameObjects that you want to survive across scene loads. Instead, create a manager scene that has all your managers and use SceneManager.LoadScene(, LoadSceneMode.Additive) and SceneManager.UnloadScene to manage your game progress. src
See the Unity guide here. Basically you would have 2 scenes open at the same time, at any given moment: the manager scene and whatever the actual active game scene is. Then you can communicate between scripts in the two open scenes via event delegates. The way it would work is:
Player selects color in scene1
Color is sent from scene1 to manager scene via event delegate
scene1 is unloaded and scene2 is loaded
Color is sent from manager scene to scene2
This is the approach I've been using for a project now which looks like this:
"0-Attract", "1-Sealant", "2-Paint", and "3-Conclusion" are my actual game scenes, and "Manager Scene" contains everything that exists in every other scene (thus no reason to kill and respawn them) as well as all of my "manager" scripts which handle the passing of data between scenes.
Note that multi-scene editing can be confusing at first, as there are new things you need to pay attention to (i.e., which scene is currently "active" and what that means) so be sure to go through the unity guide I posed above. Good luck!
you can Call DontDestroyOnLoad method on any object that you want that will remain wen you are moving the a new scene so it will not be destroyed when scene is closing.

Editing unity scene files post-compile

I'm working on a mod engine for a unity game. It is 2D based and stores all the level data in separate files ("level1", "level2") in the game data folder (I believe this is standard for unity). I am looking to be able to edit these in post to add/remove game objects in the scene. I wish to be able to do this programmatically in C#.
I've already had a look at the file in the hex editor, and it seems like this is possible (I can see basic game object data).
Currently im loading the scene then moving all objects around or instantiating new ones, but this is proving to be unstable because of the way the game handles objects.
If anyone could point me in the direction of how i would go about this, it would be greatly appreciated.
Update for those that are asking for additional info: Yes, by levels I mean scenes, unity saves them as “level0”, “level1”, etc
I am not the author of the game, the game was not designed with changing the scenes in mind, almost all of the interacatble objects have special riggers crafted to them, so in order to move them it requires me to be extremely careful or the game crashes.
From what you write it seems your separate files are actual Unity3D scenes. You should only have once scene and a system in place that reads and loads your data, for example from text files, then programmatically instantiate those object at run time.

What is the difference in scripts in unity between the Animation and Animator?

In all the scripts i used so far when changing in the script the walk speed or adding animation clip it didn't effect it at all.
To change the character speed i need to change it in the Third Person Character (Script) > Move Speed Multiplier.
And to change animation or to add animation i need to go to the Animator window and add a new state and in the state to use the HumandoidWalk then set the state as default or in the script to use this with Play like Play("Walk")
Then what all the properties in the scripts why the speed and animation and others never effect it ? (Not talking about Nav Mesh Agent or Character Transform if needed).
For example i have a script that can accept Walk anim and then i select the HumandoidWalk but that will make the character not walking at all. Only if i make the state in the animator window then it will walk.
It's not only one specific script but in others too.
I see in many places users use Animation or _animation with Play("Walk") and i to make the player move and use animation i need to use Animator or _animator.
Then what is the difference in scripts in unity between the Animation and Animator windows ? What should i use to make the character in this case ThirdPersonController to walk with animation and not just move ?
For exmaple when using waypoints i want when running the game the enemies to start walking/patrolling atuaomtic so i make new state in animator waindow with HumanoidWalk and then in the script specific for a enemy i use the Play("Walk").
Complexity and backwards compatibility.
Basically, when Unity was just created as a product, it was a pretty basic game engine, and a lot of systems that were necessary for game development were not very advanced. Then, a need for something with more capabilities arisen, and in a lot of cases, Unity decided to create a completely new system from scratch, and leave the old one as well.
Now we have legacy and new systems for GUI, for animation, for input handling, for particles and probably for something else I'm forgetting right now. However, it doesn't mean that old systems are completely useless: quite often, you want to use a simple and straightforward system without all the bells and whistles.
New animation system allows you to create great characters, but it also takes a lot of time to learn and set up. If you have a simple animated mesh that just needs to do the same animation on loop, I would use the old system; if I had a complicated character with several layers of different behaviours and animations created to blend with one another, I would use the new animator.
By the way, the same holds for UI: while the old system is pretty bad for working on player-looking UI, it's still widely used for quick prototypes and all kinds of debug menus.

Categories

Resources