Hey Jason, to fix the audio issue, you either want to make sure you are recording a mono track, or split the stereo track after it has been recorded. This can be done in Adobe Audition by selecting "Edit > Extract Channels to Mono Files". Or in Premiere, you can right-click the audio clip, select "Audio Channels" and set both the left and right channels to produce the left channel's audio. Great tutorial btw!
Have been stuck on the wrong side of a stupidly wide Valley, a C# programmer just trying to get that Ahah moment on how Unity programmatically interacts between GameObjects. This great, to the point, clear and simple tutorial at a pace that doesn’t side track down rabbit holes is a blessing. Liked, Subscribed and Grateful.
i was moments from clicking off and then you said "injection" and it perfectly described the problem i was trying to solve. With a little phanagling, I got it to work, thanks to you!
3 роки тому
I was watching your video to make a script which checks if all enemies are dead. And you gave me ideas, thank you
This video is just perfect! is solved almost all issues i was having commimg from a programming background into unity. I still find referencing things through the editor very weird instead of just importing something into the script, but i loved the Dependency Injection method, very clean/optimized solution
I just found your stuff not that long ago, but everytime I run into an issue with Unity (I'm pretty new to it, and coding) your stuff seems to have the solution. Thank you for the good tutorials
Didn't think of setting prefab's type to anything other than "GameObject" before. The way you did it here, you don't have to use "GetComponent()" to call its "Initialize". Neat, thanks!
So I've been pulling my hairs out while trying to find out how to reference in a ScriptableObject. Turns out you can't just do it the usual way, with drag and drop from hierarchy to inspector. Your second way, with FindObjectOfType works though. Thank you so much! Liked and subscribed. I hope if someone has this same issue, you'll notice this comment.
Glad to see that ScriptableObject GameObject references are fine to use. I thought doing it that way was abusing ScriptableObjects but I kept using them for references anyway since it felt right. I've been liking a mix of SO GameObject references and injections.
Oh damn, as an old MVVM guy, would have been nice to see this video when I re-entered programming scene with Unity. I didn't think dependency injection was a thing in Unity, since every tutorial and guide so far have been using either the find or singleton pattern. This should really help my brain adjust to Unity better
Sometimes I wish I could like a video twice or more! I really appreciate what you did and since you said we can ask questions, I have one: Short story: How to reference GameObjects that a ScriptableObject Depends on (needs it)? Or How to assign GameObjects in the ScriptableObject itself? Long Story: I tried and tested all 5 approaches you mentioned on my own (surely bigger project!) and I was successful running it without any errors or warnings and achieved what I wanted from my codes, EXCEPT the ScriptableObject way; I can not find a proper way to reference to an object which my ScriptableObject depends on, I tried assigning it with the first method in the video but there was no way the editor could show me the Object I needed, so I could select it; Then I decided to make the object that I need a Prefab, but I was unsuccessful again; At last, I tried to assign that object in the OnEnable method you mentioned by using the second method (.Find) and it worked at the start of the game, but whenever I wanted to access to and change the object that my ScriptableObject depends on, during a run, it said the Object is not accessible maybe because it is destroyed and suggests that I should not Destroy it or check if it is destroyed before accessing it... Thanks again and sorry for the faulty English!
Wow i like this last one,it completely bypasses the hook-up from the editor which i hate,and is impractical it doesn't scale,plus it's something that can be used outside of unity! Just great!
You may want to cover using an event system as an alternative to needing references to specific objects or scriptable objects. Most likely, it's a combination of the singleton (event manager) and observer (score manager) patterns. In this case, the bad guy scripts just raise events with the event manager without needing to know or care about who depends on that info. The observers (in this case it's the score manager, but you could have other objects that care like an audio manager, ui element, particle effects, etc) listen for events without needing to know or care about who's giving that info.
I kinda feel the same way. Of the five here, I would probably go with scriptable objects as the cleanest, but I kinda don't like Unity's way of visualizing the instances as project assets instead of in the hierarchy. I wish scriptable objects were visualized in the editor as children of the automatic dontdeleteonload instance that appears in runtime or something. Events are cleaner yet, where the bad guy just broadcasts "hey, i got hurt 1hp by Player Red" and the score manager sees this and responds "that's worth 1 score point for Player Red". The bad guy should have no idea what score is earned, maybe the player red has an earn-double powerup. If you want to add a team-rally-manager it could also hear the same damage event and decide to earn rally points for Player Red's team. Keep the separation of concerns.
@@HariEdoTV Hi. A bit too late for this but I liked how you described the event system in your comment. I'm kinda beginner/intermediate in Unity and I want to learn more about this implementation. Can you refer me some source or learning material with a similar narrative(like "bad guy broadcasts and score manager listens") as yours? I would really appreciate it.
@@AzazelezazA A bit late, but it is the observer pattern. Search for OOP Design Patterns. I think rather than using a singleton event manager, you probably just want to just have the bad guys fire an event (static unityaction) and subscribe to that event
the beauty about gimmicky 7.1 surround adapters is that in cases like this you can just turn on the fake surround and voila - no missing right channel problem. still regret buying it though
Hi Jason. Right audio channel is missing. By the way, I would love to know more about dependency injection in Unity. The only thing I know about it is that in the constructor, you would pass in dependencies, and keep your fields private, your properties read only if possible (to keep your object externally immutable). I don't know what the frameworks for dependency injection do, but I am aware of them existing (Ninject) - since I am beginner in C#. And since MonoBehaviour scripts don't use typical constructors, I am not sure how would this be done in Unity.
From what I have seen you extend the monobehavior class and add in the Dependency injection. So when you Instantiate it requires some object that passes dependancies in like arguments. More complex methods use subscriptions and events or a central object registry. I personally just like the Instantiate method being bolted on to MB components since it functions so much like we are used to with Prefabs. (Its not the same, but feel is better than actual) And if I am building concrete services than Singletons are the go to.
I highly recommend against using a DI framework in Unity. Its going to revolve around using a master class to inject dependencies, which not only obscures relationships for any team members that cant dig through code, but also requires you to maintain that long list of dependencies. A delegate event system for universal messages and simple Unity serialized field drag and drop injection for more direct relationships will keep your project far more decoupled, maintainable, and user friendly. Or in other words: need to add a point to the score? Send a "point scored" message out into the world. Let anything that cares do what it will (maybe the sound manager will be an observer, and play a cheering sound). Need player 2 to use a different input manager, or maybe you want to test a new implementation? Drag and drop the correct input manager into the slot in the editor. What you don't want to do is have to open up a class that has a long list of interfaces and hard coded connections to various scripts that you have to manipulate/add logic to in order to inject simple dependencies like the above.
Sorry for late response, but I don't think you are using dependency injection correctly (or maybe I am mixing the concept a bit with a different model) if you are using it like that. The dependency injection model is actually meant to simplify and streamline the logic, validation away from the interface and who uses the data. And ofc. it should be used in conjugation with OnChanged events to propagate the changes from a centralized place to all subscribers. This approach allows you to create an extra layer of decoupling (used a lot it MVVM model in particular that is based on dependency injection of the ViewModel, that acts as an interface between the view and the model to make sure things don't break if view or the model is changed), so you can change the underlying database, storage system etc, without breaking anything in the actual program/game, and create the visual (view) part of the game, without caring how the data is actually handled, you just want to know what is the interface you connect to, and everything should work automagically, no matter where or how that underlying data is changed
Some big gotchas with singletons in Unity are: remember to null out the instance reference in OnDestroy() and to mark the game object as don't destroy on load. If you don't do this, things can get messy with scene loading and PIE mode.
just as a curiosity , do you have games on playstore or appstore? any names / links ? do you have any books that you released or recommend ? thanks for making videos like this one :)
Unity has an internal thing right now that they use for workshops with a customizable editor window indicating steps in a tutorial. You can select any step and it will load a different "state" of the project. They essentially keep multiple copies of the project in a designated folder on the same folder layer as the Assets folder. That way you can jump to different states in a tutorial without having to name tutorial scripts with their step #. It's also useful for live tutorials when people join in late and have to start at a certain step and they can just follow along from that point instead of not being able to catch up due to the steps they've missed.
Nice tutorial. I would only add one small alternative to Singleton-> using static methods and static properties. Can be more adjustable than singleton in certain scenarios
Another great way is c# events and delegates. They are actually nearly best in this scenarios. Ofc in my opinion. Though injection makes sense to me. I love hard to break code and less editor interaction.
AHEM - I am quite an expert, but like to see what others are saying. HOWEVER If your going to teach people to use FindObjectOfType; for the love of coding make sure to do it right. Add a local reference. Unity supposedly does a good job of cache that, but imagine hundreds of find object calls. And for a 6th way, I believe, similar to your Instance, you can just use a Static Variable, and/or function. Keep up the good work, regardless.
Thanks a lot for this explanation. Was really simple, good and showed all the code already written and working. The only problem is that Input.GetbuttonDown doesnt work for sevral buttons, since it activates all. Showing how to add a OnClick funciton via script instead would have been great, Since this would make it usable for UI and 2D UI based games as well.
What about events? Right now my preferred method is by using an event bus... Its not exactly a reference method, but a communicate method... its beautiful... pure programming magic :D
great video, thank you! Since you asked, I do have one question, fairly beginner unity dev here. In what situations is it preferable to use a script to assign classes to variables, versus doing it in the editor itself just by dragging and dropping? When I drag and drop stuff into variables, it kinda feels like the easy way out, but is it actually better sometimes?
So what is more performant, in the case that you have 30 to 100 classes, which each one only have one instance and you are going to be calling them from other classes multiple times in runtime, FindObjectOfType or Singleton?
What do you think about just sending event system from object or whatever you want, to score manager? You also can create interface with such event and just implement it on different objects, which can change score. Thanks.
Hi Jason, can you also make a video on how to reference components on other game objects from a script on a prefab. You cant just drag components onto a script thats in a prefab. I find it very confusing.
Jason help me out, where did I put the variables that will be need in all scenes like level,xp,items on inventory? By now Im using a script with dont destroy on load but I imagine that there’s a better way...
Hi Jason,I have a music player that lets you lets you play next and previous track, stop and mute, with current track name and time left. I have multiple levels so I put a do not destroy on load on it. Yes the music plays, but it doesn't bring in the ui text from last scene..so I can't change the track etc. How would I get round this problem?..thanks
Oh, wowwwwww Bruthah, I needed to here what you said about injections and Scriptable objects, I just started with unity recently. I have had NO CONFIRMATION I was doing things the right way or classes just self education. to bad self education lacks confirmation. The bonus part of what you said; " takes a little bit more brain power!". That statment made me feel Awesome, that I understand the way they work with one another. They have a crazy learning curve for sure and can almost feel like playing a game of chess makes you feel when you know your beat. They to take a bit more of creativity in thier usage as a project implementing scriptable object grows larger. THANK YOU SO MUCH, IM GONNA SUBSCRIBE AND BINGE WATCH ONLY YOUR CHANNEL FOR A WHILE. THIS IS MY NEW PEER GROUP
is there a way to reference a child of an object by the child's name? I have a camera that is going to be used for aiming down sights of different guns but I need a way to make the camera move the a child of the gun so that it will position according to how I customize each gun's aiming view. This is for procedural guns that will have scopes that will vary in size and shape meaning I need to set a camera position for each scope variant. I would add a camera to each scope and then call the camera but that becomes problematic when multiple scopes are present in the scene.
Including installing and an explanation of how unity accesses and jobs, and where jobs are saved and defined on the system. The whole concept is blurry to me :(
What about referencing a certain instance when I have multiple instances of the same object? How would you reference a particular one of those instances? For example, I'm making a dice game in Unity where I instantiate 5 dice with a for loop. I want to be able to sort those dice's positions based on the side value rolled. How would I reference a certain one that I need?
6:57 Sorry but scriptable objects to change values at runtime only works when you play it in Editor. When you make a build out of it, it doesn't work. You can't actually change anything. So i'm a bit confused why you chose that as an example.
Any help with the following would be really appreciated: 1. Getting a reference means you get that one object? meaning, If I instantiate Enemies and then inject them with a Health Script and I shoot one enemy or make a modification to either enemy do the Health on both changes? 2. Is it better to have a prefab with components or a scriptable object with a reference to a prefab? An example would be a Heroes collector game or a pokemon game, where you would make a scriptable object of the type "Hero" and then plug in scripts for Health, Level, etc. or would it be better to have a prefab that has a reference to a scriptable object.? I am just really confused on the method behind creating a "Hero" or "Pokemon" since a ton of RPGs focus on party size. 3. Does the reference gets copied or mimic? example, If I have reference to a List and My player gets a hero. I will add that hero to the List my player has of obtained heroes. But then, let us say in an stage the Hero fights another one of itself. will any changes I make to the player's hero be mimic by the hero in the List. How I think of it is this way( List hero gets a hero and now List playersHero = hero so that means if playersHero is the same as hero If I modify playersHero, hero has to change cause they are the same I am just referencing a reference.... that is all. I am stuck at home so I gave myself until the Quarantine ends to make a portfolio and apply. I came across your Videos not long ago, maybe 2 weeks and I have been hooked. I don't get a lot of vocab but every piece of advice is great. I just wish I didn't miss your Q&A's. Unity is great and in the last 2 weeks, I have gotten over my fear of code. Thank you! also, I thought you were Dan Harmon for a while, especially in your newer videos.
This is very helpful and well presented. I've been learning from a text that's presented the first 3 of your techniques, but without much context and no comparison. I learned from this how scriptable objects can give persistent scores. I do need that, as I will have multiple scenes and levels with scores and health. Clearly, scriptable objects, perhaps in conjunction with dependencies injection is an approach that works. Any downsides? But I'm unclear about whether any of the other methods can be made to work across scenes and levels, and how. You've said "not the first", but what about the two others? Can you expand on this? Others have talked about using a GameStateManager (like your scoremanager) with static fields and attached to something like the camera and that that will work across scenes and levels. Will it? Comments about this approach?
Hey Jason, are you going to be covering ECS at all? I'm looking through it myself and it seems like learning it will greatly help decouple and structure code. Also the performance is great =D
Sorry but here comes another question: Does using Var instead of the actual type of the Object have any benefits? (approach 5, in Instantiating the badGuyPrefab) Like: var badGuy = Instantiate(badGuyPrefab); VS ClickToKill4 badGuy = Instantiate(badGuyPrefab);
No, var is just shorthand My personal rule is simple known datatypes should be spelled out: int three = 5; var three = 5; But when readability gets harmed and/or it's an uncommon datatype and the variable name is clear, use var var items = new Dictionary(); Dictionary items = new Dictionary();
how can I identify a private object? for example, in the scene there is a cube, and I want to make a reference of it: -------------------------------------------------- ---------------------------- private GameObject Cube; void Start { Cube = GameObject ("cube");
Use [SerializeField] if the object is within the scene and drag-drop gameobject to the serialized field within the editor. If the object is instantiated at run-time, you'll need to use one of the methods that the video describes above.
Forgive me, Im new to OOP and Unity... Why are none of these classes in your examples ever being newed up. I see in most of the examples scoremanager.addscore is called, but I never see a new instance of scoremanager created in clicktokill. Am I missing something? In one instance you used a static, but in the other 4 I didnt see any static members.
hi, can you plz do a video showing how to do a board game on unity, where can the character choose to go or not to the other way? like a square with a line on de midle where they walk on it based on a dice movement..it would be really amazing ;v;
Hello I have an issue whe n I write FindObjectOfType() it's nto getting any game object in the scene means i can't refer to game object in the scene from script any solution please ?
Hello. FindObjectOfType is used for finding a script or other component and there is only one of them in the scene. To search for an object by name, use GameObject.Find("name"). For example: GameObject cube; then in Start function: cube = GameObject.Find("name");
0:00 Editor hook-up
1:51 FindObjectOfType
2:59 Singleton
4:46 ScriptableObject
7:12 Dependancy Injection
Densetsu no hīrō , arigatogojaimas
Thanks!
You're an angel :P
This Needs to be pinned
which one is best?
The right channel is missing because it is about mono behaviors.
underrated comment lmao
@@thenecromancer4113 Thank you 😀
Hey Jason, to fix the audio issue, you either want to make sure you are recording a mono track, or split the stereo track after it has been recorded. This can be done in Adobe Audition by selecting "Edit > Extract Channels to Mono Files". Or in Premiere, you can right-click the audio clip, select "Audio Channels" and set both the left and right channels to produce the left channel's audio.
Great tutorial btw!
Have been stuck on the wrong side of a stupidly wide Valley, a C# programmer just trying to get that Ahah moment on how Unity programmatically interacts between GameObjects. This great, to the point, clear and simple tutorial at a pace that doesn’t side track down rabbit holes is a blessing. Liked, Subscribed and Grateful.
i was moments from clicking off and then you said "injection" and it perfectly described the problem i was trying to solve. With a little phanagling, I got it to work, thanks to you!
I was watching your video to make a script which checks if all enemies are dead.
And you gave me ideas, thank you
This video is just perfect!
is solved almost all issues i was having commimg from a programming background into unity. I still find referencing things through the editor very weird instead of just importing something into the script, but i loved the Dependency Injection method, very clean/optimized solution
I just found your stuff not that long ago, but everytime I run into an issue with Unity (I'm pretty new to it, and coding) your stuff seems to have the solution. Thank you for the good tutorials
Didn't think of setting prefab's type to anything other than "GameObject" before. The way you did it here, you don't have to use "GetComponent()" to call its "Initialize". Neat, thanks!
So I've been pulling my hairs out while trying to find out how to reference in a ScriptableObject. Turns out you can't just do it the usual way, with drag and drop from hierarchy to inspector. Your second way, with FindObjectOfType works though. Thank you so much! Liked and subscribed.
I hope if someone has this same issue, you'll notice this comment.
Every beginner should watch this
Was wondering about these for the entire last week. Thanks a ton!
Glad to see that ScriptableObject GameObject references are fine to use. I thought doing it that way was abusing ScriptableObjects but I kept using them for references anyway since it felt right. I've been liking a mix of SO GameObject references and injections.
Oh damn, as an old MVVM guy, would have been nice to see this video when I re-entered programming scene with Unity. I didn't think dependency injection was a thing in Unity, since every tutorial and guide so far have been using either the find or singleton pattern. This should really help my brain adjust to Unity better
Had to add this vid to my must watch list. Great tut
Holy crap number four is a game-changer for me! Thank you.
Sometimes I wish I could like a video twice or more!
I really appreciate what you did and since you said we can ask questions, I have one:
Short story:
How to reference GameObjects that a ScriptableObject Depends on (needs it)?
Or
How to assign GameObjects in the ScriptableObject itself?
Long Story:
I tried and tested all 5 approaches you mentioned on my own (surely bigger project!) and I was successful running it without any errors or warnings and achieved what I wanted from my codes, EXCEPT the ScriptableObject way;
I can not find a proper way to reference to an object which my ScriptableObject depends on,
I tried assigning it with the first method in the video but there was no way the editor could show me the Object I needed, so I could select it; Then I decided to make the object that I need a Prefab, but I was unsuccessful again; At last, I tried to assign that object in the OnEnable method you mentioned by using the second method (.Find) and it worked at the start of the game, but whenever I wanted to access to and change the object that my ScriptableObject depends on, during a run, it said the Object is not accessible maybe because it is destroyed and suggests that I should not Destroy it or check if it is destroyed before accessing it...
Thanks again and sorry for the faulty English!
Valuable information for the beginner. I find myself doing the direct reference, not anymore after this video.
Wow i like this last one,it completely bypasses the hook-up from the editor which i hate,and is impractical it doesn't scale,plus it's something that can be used outside of unity!
Just great!
This is one of my favorite of your videos. Thank you!
You may want to cover using an event system as an alternative to needing references to specific objects or scriptable objects. Most likely, it's a combination of the singleton (event manager) and observer (score manager) patterns. In this case, the bad guy scripts just raise events with the event manager without needing to know or care about who depends on that info. The observers (in this case it's the score manager, but you could have other objects that care like an audio manager, ui element, particle effects, etc) listen for events without needing to know or care about who's giving that info.
I kinda feel the same way.
Of the five here, I would probably go with scriptable objects as the cleanest, but I kinda don't like Unity's way of visualizing the instances as project assets instead of in the hierarchy. I wish scriptable objects were visualized in the editor as children of the automatic dontdeleteonload instance that appears in runtime or something.
Events are cleaner yet, where the bad guy just broadcasts "hey, i got hurt 1hp by Player Red" and the score manager sees this and responds "that's worth 1 score point for Player Red". The bad guy should have no idea what score is earned, maybe the player red has an earn-double powerup. If you want to add a team-rally-manager it could also hear the same damage event and decide to earn rally points for Player Red's team. Keep the separation of concerns.
@@HariEdoTV Hi. A bit too late for this but I liked how you described the event system in your comment. I'm kinda beginner/intermediate in Unity and I want to learn more about this implementation. Can you refer me some source or learning material with a similar narrative(like "bad guy broadcasts and score manager listens") as yours? I would really appreciate it.
@@AzazelezazA A bit late, but it is the observer pattern. Search for OOP Design Patterns. I think rather than using a singleton event manager, you probably just want to just have the bad guys fire an event (static unityaction) and subscribe to that event
Jason is truly under-rated
my right ear gave this a thumbs down.
the beauty about gimmicky 7.1 surround adapters is that in cases like this you can just turn on the fake surround and voila - no missing right channel problem.
still regret buying it though
for me left
@@Grempington You saved my life
I spent about 2 minutes going through my sound settings, always start with the simplest possible cause.
same
ive been looking for that for days now. thank you!
Hi Jason. Right audio channel is missing.
By the way, I would love to know more about dependency injection in Unity.
The only thing I know about it is that in the constructor, you would pass in dependencies, and keep your fields private, your properties read only if possible (to keep your object externally immutable). I don't know what the frameworks for dependency injection do, but I am aware of them existing (Ninject) - since I am beginner in C#.
And since MonoBehaviour scripts don't use typical constructors, I am not sure how would this be done in Unity.
From what I have seen you extend the monobehavior class and add in the Dependency injection. So when you Instantiate it requires some object that passes dependancies in like arguments.
More complex methods use subscriptions and events or a central object registry.
I personally just like the Instantiate method being bolted on to MB components since it functions so much like we are used to with Prefabs. (Its not the same, but feel is better than actual)
And if I am building concrete services than Singletons are the go to.
I highly recommend against using a DI framework in Unity. Its going to revolve around using a master class to inject dependencies, which not only obscures relationships for any team members that cant dig through code, but also requires you to maintain that long list of dependencies.
A delegate event system for universal messages and simple Unity serialized field drag and drop injection for more direct relationships will keep your project far more decoupled, maintainable, and user friendly.
Or in other words: need to add a point to the score? Send a "point scored" message out into the world. Let anything that cares do what it will (maybe the sound manager will be an observer, and play a cheering sound). Need player 2 to use a different input manager, or maybe you want to test a new implementation? Drag and drop the correct input manager into the slot in the editor.
What you don't want to do is have to open up a class that has a long list of interfaces and hard coded connections to various scripts that you have to manipulate/add logic to in order to inject simple dependencies like the above.
Sorry for late response, but I don't think you are using dependency injection correctly (or maybe I am mixing the concept a bit with a different model) if you are using it like that. The dependency injection model is actually meant to simplify and streamline the logic, validation away from the interface and who uses the data. And ofc. it should be used in conjugation with OnChanged events to propagate the changes from a centralized place to all subscribers.
This approach allows you to create an extra layer of decoupling (used a lot it MVVM model in particular that is based on dependency injection of the ViewModel, that acts as an interface between the view and the model to make sure things don't break if view or the model is changed), so you can change the underlying database, storage system etc, without breaking anything in the actual program/game, and create the visual (view) part of the game, without caring how the data is actually handled, you just want to know what is the interface you connect to, and everything should work automagically, no matter where or how that underlying data is changed
Love the video as I was having problems with too many singletons and the injection method will solve some of my problems
Some big gotchas with singletons in Unity are: remember to null out the instance reference in OnDestroy() and to mark the game object as don't destroy on load. If you don't do this, things can get messy with scene loading and PIE mode.
just as a curiosity , do you have games on playstore or appstore? any names / links ? do you have any books that you released or recommend ? thanks for making videos like this one :)
Unity has an internal thing right now that they use for workshops with a customizable editor window indicating steps in a tutorial. You can select any step and it will load a different "state" of the project. They essentially keep multiple copies of the project in a designated folder on the same folder layer as the Assets folder. That way you can jump to different states in a tutorial without having to name tutorial scripts with their step #. It's also useful for live tutorials when people join in late and have to start at a certain step and they can just follow along from that point instead of not being able to catch up due to the steps they've missed.
This was really informative, loving the regular content. Keep at it, it helps me out a lot. Thanks!
Really loved this one, it cleared my way, thank you!
Wow scriptable objects look like just sweet candy
Very good explanation and video.
Thanks it was worth alone to just know about the method names
VERY WELL DONE Jason!
I wish I could give more than one like. You are an awesome man.
Nice, many ways to do the same thing is always a good thing! Each one may be best for a different project usage.
Thank you for this video.
Nice tutorial. I would only add one small alternative to Singleton-> using static methods and static properties. Can be more adjustable than singleton in certain scenarios
Thank u Jason this helped me allot.
Another great way is c# events and delegates. They are actually nearly best in this scenarios. Ofc in my opinion. Though injection makes sense to me. I love hard to break code and less editor interaction.
My left ear enjoyed thus.
Very helpful!
thumbs up for the DI
thanks jason you are perfect !
Great video, helped a looooot BD
So much interesting ideas in one day :)
I just wish you could use dependency injection in a constructor on a mono behavior
This video helps me a lot! Thank you!!!!!!!!!
AHEM - I am quite an expert, but like to see what others are saying. HOWEVER
If your going to teach people to use FindObjectOfType; for the love of coding make sure to do it right. Add a local reference. Unity supposedly does a good job of cache that, but imagine hundreds of find object calls.
And for a 6th way, I believe, similar to your Instance, you can just use a Static Variable, and/or function.
Keep up the good work, regardless.
fantastic for noobs like me
thanks x 10000
Its is Very Usefull!!
Thanks!!
Thanks a lot for this explanation. Was really simple, good and showed all the code already written and working.
The only problem is that Input.GetbuttonDown doesnt work for sevral buttons, since it activates all.
Showing how to add a OnClick funciton via script instead would have been great, Since this would make it usable for UI and 2D UI based games as well.
Hey man you are amazing
Noice, my left ear liked that.
What about events? Right now my preferred method is by using an event bus... Its not exactly a reference method, but a communicate method... its beautiful... pure programming magic :D
You are the best
great video, thank you! Since you asked, I do have one question, fairly beginner unity dev here.
In what situations is it preferable to use a script to assign classes to variables, versus doing it in the editor itself just by dragging and dropping? When I drag and drop stuff into variables, it kinda feels like the easy way out, but is it actually better sometimes?
Hi there Jason... about the naming convention... does it proper to say the first method is 'direct' and all the others 'indirect'? Thanks :)
So what is more performant, in the case that you have 30 to 100 classes, which each one only have one instance and you are going to be calling them from other classes multiple times in runtime, FindObjectOfType or Singleton?
Could you do explain more about the last method? That was very cool. Thank you
LOVE YOU SO MUCH MY BOY :))))))))))
What do you think about just sending event system from object or whatever you want, to score manager? You also can create interface with such event and just implement it on different objects, which can change score. Thanks.
Great idea, did a video on it today :)
Great Keep It up
Hi Jason, can you also make a video on how to reference components on other game objects from a script on a prefab. You cant just drag components onto a script thats in a prefab. I find it very confusing.
I'm struggling with this as well
Well done
Jason help me out, where did I put the variables that will be need in all scenes like level,xp,items on inventory? By now Im using a script with dont destroy on load but I imagine that there’s a better way...
Excelent.
Hi Jason,I have a music player that lets you lets you play next and previous track, stop and mute, with current track name and time left. I have multiple levels so I put a do not destroy on load on it. Yes the music plays, but it doesn't bring in the ui text from last scene..so I can't change the track etc. How would I get round this problem?..thanks
Oh, wowwwwww Bruthah, I needed to here what you said about injections and Scriptable objects, I just started with unity recently. I have had NO CONFIRMATION I was doing things the right way or classes just self education. to bad self education lacks confirmation. The bonus part of what you said; " takes a little bit more brain power!". That statment made me feel Awesome, that I understand the way they work with one another. They have a crazy learning curve for sure and can almost feel like playing a game of chess makes you feel when you know your beat. They to take a bit more of creativity in thier usage as a project implementing scriptable object grows larger. THANK YOU SO MUCH, IM GONNA SUBSCRIBE AND BINGE WATCH ONLY YOUR CHANNEL FOR A WHILE. THIS IS MY NEW PEER GROUP
you should probably use "DontDestroyOnLoad" for singletons
Thanks for the video! May I know what does the "this" keyword mean in this context? I only know "this" can be used in the class constructor, thanks!
this always refers to the current class where you are using this.
@@nikilkumar1000 Thank you so much!
Awesome !!
is there a way to reference a child of an object by the child's name? I have a camera that is going to be used for aiming down sights of different guns but I need a way to make the camera move the a child of the gun so that it will position according to how I customize each gun's aiming view. This is for procedural guns that will have scopes that will vary in size and shape meaning I need to set a camera position for each scope variant. I would add a camera to each scope and then call the camera but that becomes problematic when multiple scopes are present in the scene.
will you cover the new c# jobs?
pleease!!!
That would be amazing !
Including installing and an explanation of how unity accesses and jobs, and where jobs are saved and defined on the system. The whole concept is blurry to me :(
What about referencing a certain instance when I have multiple instances of the same object? How would you reference a particular one of those instances? For example, I'm making a dice game in Unity where I instantiate 5 dice with a for loop. I want to be able to sort those dice's positions based on the side value rolled. How would I reference a certain one that I need?
6:57 Sorry but scriptable objects to change values at runtime only works when you play it in Editor. When you make a build out of it, it doesn't work. You can't actually change anything. So i'm a bit confused why you chose that as an example.
Jason great tutorial but I didnt get how 1st method will not work for multiple bad guy??
It will always find the first one
Awesome video ad always :)
Scriptable objects are good
Any help with the following would be really appreciated:
1. Getting a reference means you get that one object? meaning, If I instantiate Enemies and then inject them with a Health Script and I shoot one enemy or make a modification to either enemy do the Health on both changes?
2. Is it better to have a prefab with components or a scriptable object with a reference to a prefab? An example would be a Heroes collector game or a pokemon game, where you would make a scriptable object of the type "Hero" and then plug in scripts for Health, Level, etc. or would it be better to have a prefab that has a reference to a scriptable object.? I am just really confused on the method behind creating a "Hero" or "Pokemon" since a ton of RPGs focus on party size.
3. Does the reference gets copied or mimic? example, If I have reference to a List and My player gets a hero. I will add that hero to the List my player has of obtained heroes. But then, let us say in an stage the Hero fights another one of itself. will any changes I make to the player's hero be mimic by the hero in the List. How I think of it is this way(
List hero
gets a hero and now
List playersHero = hero
so that means if playersHero is the same as hero If I modify playersHero, hero has to change cause they are the same I am just referencing a reference....
that is all. I am stuck at home so I gave myself until the Quarantine ends to make a portfolio and apply. I came across your Videos not long ago, maybe 2 weeks and I have been hooked. I don't get a lot of vocab but every piece of advice is great. I just wish I didn't miss your Q&A's. Unity is great and in the last 2 weeks, I have gotten over my fear of code. Thank you!
also, I thought you were Dan Harmon for a while, especially in your newer videos.
This is very helpful and well presented. I've been learning from a text that's presented the first 3 of your techniques, but without much context and no comparison.
I learned from this how scriptable objects can give persistent scores. I do need that, as I will have multiple scenes and levels with scores and health. Clearly, scriptable objects, perhaps in conjunction with dependencies injection is an approach that works. Any downsides?
But I'm unclear about whether any of the other methods can be made to work across scenes and levels, and how. You've said "not the first", but what about the two others? Can you expand on this?
Others have talked about using a GameStateManager (like your scoremanager) with static fields and attached to something like the camera and that that will work across scenes and levels. Will it? Comments about this approach?
Hey Jason, are you going to be covering ECS at all? I'm looking through it myself and it seems like learning it will greatly help decouple and structure code. Also the performance is great =D
Sorry but here comes another question:
Does using Var instead of the actual type of the Object have any benefits? (approach 5, in Instantiating the badGuyPrefab)
Like:
var badGuy = Instantiate(badGuyPrefab);
VS
ClickToKill4 badGuy = Instantiate(badGuyPrefab);
No, var is just shorthand
My personal rule is simple known datatypes should be spelled out:
int three = 5;
var three = 5;
But when readability gets harmed and/or it's an uncommon datatype and the variable name is clear, use var
var items = new Dictionary();
Dictionary items = new Dictionary();
@@PerfectlyNormalBeast Beside the fact that I already found my answer through these 5 months, your caring and answer is appreciated.
In this special case wouldn't it be the most efficient to just have ScoreManager be a static class with a static function AddScore()?
@ 5:28 What did you click or typed that lead you to the script?
Hi , I don't want repeat (coins) When restart game please how do I do that , and thank you
My left ear enjoyed this.
Using the findobjectoftype is expensive right?
How do i pass a custom parameter to just instantiated object usung dependency injection (not using getcomponent) Anyone help pls?
New video about Addressables, asset bundles, and lastly resources plz :D
1:04 how to go to another script like them? (Which keys)
ctrl + tab
@@28082989 ty
how can I identify a private object?
for example, in the scene there is a cube, and I want to make a reference of it:
-------------------------------------------------- ----------------------------
private GameObject Cube;
void Start {
Cube = GameObject ("cube");
Use [SerializeField] if the object is within the scene and drag-drop gameobject to the serialized field within the editor. If the object is instantiated at run-time, you'll need to use one of the methods that the video describes above.
Thank you!
Forgive me, Im new to OOP and Unity... Why are none of these classes in your examples ever being newed up. I see in most of the examples scoremanager.addscore is called, but I never see a new instance of scoremanager created in clicktokill. Am I missing something? In one instance you used a static, but in the other 4 I didnt see any static members.
hi, can you plz do a video showing how to do a board game on unity, where can the character choose to go or not to the other way? like a square with a line on de midle where they walk on it based on a dice movement..it would be really amazing ;v;
My left ear got a lot of information
true
Why is Dan Harmon doing videos on unity?
How can I get a reference for this?
However you can just get it!
Hello I have an issue whe n I write FindObjectOfType() it's nto getting any game object in the scene means i can't refer to game object in the scene from script any solution please ?
Hello. FindObjectOfType is used for finding a script or other component and there is only one of them in the scene. To search for an object by name, use GameObject.Find("name"). For example: GameObject cube; then in Start function: cube = GameObject.Find("name");
there is no pub-sub ?