Me and my friend are building a simple idle game as our first project in unity.
We got to the point at which we build a script that handles all calculations, and communicates with each GameObject the player sees.
Now, to what should we attach this script?
We would rather not use a GameObject because:
1) The GameObject would be an overkill
2) The GameObject lies in the space where all of my "physical" objects exist (That would be useless since the script does not have to "exist somewhere")
First time posting in stackoverflow, if I made any mistake please tell me ^^
Convention says to put it on the main camera or an empty GameObject.
If you really don't want to do either of those, you could make your class static.
If your class inherits from MonoBehaviour you have to attach it to a GameObject. If you want to attach it to a GameObject it has to inherit from MonoBehaviour.
If you want to better understand the Unity way, I advise you to read up on Entity-Component Systems:
GameObject are Entities
MonoBehaviour are Components
Services and managers generally get implemented as Components in Unity which then get attach to the main camera or to empty GameObjects named after them.
You can create a usual singleton class, and "Game Initializer" monobehaviour, that will initialize all your singletone managers, fabrics, etc...
Just add this monobehaviour to every scene in empty gameobject with a code like that:
if(GameManager.instance == null)
new GameManager();
Or in case of scriptableObjects,
if(GameManager.instance == null)
gameManager.init()
Also, a good desicion is to use Entity System pattern, here is frameworks for unity (Unity-Ash, Entitas, etc)
Related
Whenever I need to instantiate prefabs, I always use the following technique
Add a GameObject/MonoBehaviour field to the MonoBehaviour which would instantiate the prefab.
Use this field to instantiate the prefab
E.g.:
public class ExmapleController : Monobehaviour
{
[Serializefield]
GameObject _examplePrefab;
GameObject _exampleObject;
public void SpawnExample()
{
_exampleObject = Instantiate(_examplePrefab);
}
}
Now my problem with this is that first of all I need two references. One for the prefab and one for the instantiated object. Secondly, whenever I need additional prefabs I have to create new and new references inside my script. This sometimes clutters my Scripts and I always wonder that is this memory efficient? What does Unity do behind the scenes? It just only stores a GUID reference for my prefab than somehow loads this prefab based on this reference? Or when I make a prefab reference the whole object tree is pulled into memory and I should rather use Asset bundles or another Unity technique?
Basically that's fine. But if you want to improve your workflow you can try creating ScriptableObjects to store the prefab references and then have only 1 reference to your scriptable object inside the mono behaviour. It will help you keep your mono behaviour classes clear. For more advanced approach you can check Dependency Injection frameworks for Unity. The most common one is Zenject. It helps you in managing dependencies in your classes (prefabs in your case are dependencies of Monobehaviour class).
When you create a reference to a Prefab Unity only store the GUID of the prefab you referenced. It is how resource system in Unity works. But if your prefab is required on the current scene then it will be fully loaded with all inner dependencies. If loading takes much time than it is the reason to think about using Addressables (AssetBundles). Unity doesnt have any other technique to avoid loading the full object tree in memory. But addressables let you control when to load the specific prefab from asset bundles to memory.
Yes this perfectly fine and how it's done. A reference is just that: a reference. The only time a copy of the object is made is when you call Instantiate.
If you have multiple prefabs that are just variations of one you should use ScriptableObject instead and have just 1 prefab that can take any variation form. You would still intantiate them the same way.
Basically that's fine. But if you want to create a huge numbe of objects or delete them you could also have a look at Object Pooling. More infos can be found under the link from the tutorial at Unity:
https://learn.unity.com/tutorial/introduction-to-object-pooling#
"Object Pooling is a great way to optimize your projects and lower the
burden that is placed on the CPU when having to rapidly create and
destroy GameObjects. It is a good practice and design pattern to keep
in mind to help relieve the processing power of the CPU to handle more
important tasks and not become inundated by repetitive create and
destroy calls. (Unity)"
I'm new to Unity's Mutliplayer system and I don't really know how to do a specific thing.
Players can have multiples weapons. When the player change weapon, an object is instantied from the resources only client-side. The thing is that a weapon needs to spawn object(s), with reference(s) and use them after. I made all weapons inherit from NetworkBehaviour and tried to call [Command] methods or [ClientRpc] method but get many errors or it just worked for one player.
The only thing I did well was to take a weapon code and execute it from a player script but move all the code from all weapons to a script on the player doesn't make any sense.
I know now that I can't call [Command] from my weapons so how I'm supposed to make those things work with the server ?
So I have a simple local multiplayer game using the new input system. There's the game scene and the results scene. Once the game ends, the results scene is loaded and you can press "start" to restart the game (load the game scene again).
The problem is, after I reload the game scene and then trigger an InputAction (seems to be only on context.started):
MissingReferenceException while executing 'PlayerInput.onActionTriggered' callbacks
MissingReferenceException: The object of type 'Rigidbody2D' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
The weird thing is, none of these supposedly missing references are actually missing. The players still move using the "missing" rigidbody and etc.
I apologize if I don't explain well. I'm new to Unity and quite lost. I'm happy to share screenshots or snippits of my code I honestly just don't know where to look for this.
You should make null all action events in start
Term
This is a common problem for developers using Unity. This is a problem that occurs so often that Unity Techonology has already given it a term to explain it.
'Fake Null'
unity blog link
https://blog.unity.com/technology/custom-operator-should-we-keep-it
In short, this happens because Unity's Object is a wrapper class that indicate pointer.
Code of Conduct
You can understand the details by reading the article on the Unity blog.
Unity developers need to be careful when using C#'s nullable syntax. (?, ??...etc)
Example Code
public class MyClass : Monobehaviour
{
[SerializeField] private Rigidbody _rigidbody;
private void DoSomething()
{
// This can cause Runtime error especially when GameObject that has
// Rigidbody already been Destroyed.
_rigidbody?.CallSomeMethod();
// This is Defensive Code preventing Runtime error
if (_rigidbody != null)
{
_rigidbody.CallSomeMethod();
}
}
}
I've got a basic AI script where I'll assign an object for the enemy to chase.
The problem is, when I turn the enemy into a prefab, the target assignment becomes blank and I cannot alter it while it's a prefab. I've tried to assign the object within the code itself but I'm not completely sure how to do so (I've tried a number of things but nothing has panned out).
Any tips on how to fix the first problem, or just how to assign a target within code would be very helpful. JavaScript would be the preferred language for code.
It's the intended behaviours of prefabs.
You can't link a gameobject which belongs to the scene, to a field of a prefab in your assets since it must be completely independant from any instance of your scene. Prefabs are intended to be instantiated, and then, you will be able to assign (through code) the public field you want to your instantiated enemy. See a prefab as a file in your HDD you can instantiate from.
Without any code it's hard to help, but I guess you can do something similar to this :
var newEnemy : EnemyAI = Instantiate(enemyPrefab);
newEnemy.target = GameObject.FindWithTag ("Player").GetComponent.<Transform>();
I'm not fluent at all with Unity script.
You are still able to "instantiate" the enemies directly in the scene by dragging and dropping the prefab into your scene, and you will be able to assign the Target of your instantiated prefab.
In my unity3D project I have a manager script that should take care of initializing a game level, instantiating and placing all relevant prefabs. However, I can't find a way to instantiate a prefab by its class - it turns out that "instantiate" really means something more like "clone" in Unity3D. So as a workaround, I had to make my manager script a MonoBehaviour, attach it to an empty object and assign my prefabs to it from the inspector since it is the only way I have found to make it work at all. This looks kind of awkward. Is there any way to actually instantiate a prefab, without cloning it from a reference?
You can load prefabs at runtime. Just have a look at the script reference which comes with examples:
http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html
Here is an article that should clarify how it works:
http://docs.unity3d.com/Documentation/Manual/LoadingResourcesatRuntime.html